安卓利用RecyclerView写一个卡片依次左右排列的页面

这是学习完《第一行代码》前三章后做的一个练习
安卓利用RecyclerView写一个卡片依次左右排列的页面_第1张图片
效果如下所示:
安卓利用RecyclerView写一个卡片依次左右排列的页面_第2张图片
接下来是代码:

首先是主布局

利用recyclerview



    

左右卡片布局



    //左侧
    

        
        
        
        
    

    //右侧
    
        
        
        
        
    

卡片信息类

package com.example.roompractice;

public class House {
    private String info,price,cost;//卡片信息,单价,总价
    private int type;//卡片的左右
    public House(String info,String price,String cost,int type)//构造器
    {
        this.info = info;
        this.price = price;
        this.cost = cost;
        this.type = type;
    }
    //返回左右类型
    public int getType()
    {
        return type;
    }
    public String[] getContent()
    {
        String[] ans = {info,price,cost};
        return ans;
    }
}

RecyclerView的适配器类

package com.example.roompractice;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import org.w3c.dom.Text;

import java.util.List;

public class HouseAdapter extends RecyclerView.Adapter<HouseAdapter.ViewHolder> {
    private List<House> houseList;
    private final int LEFT = 1;//左
    private final int RIGHT = 0;//右
    static class ViewHolder extends RecyclerView.ViewHolder
    {
        public ViewHolder(View v)
        {
            super(v);
        }
    }

    //写成两个左右ViewHolder的原因是如果卡片很多,我们只需要选择
    //其中一个布局进行加载就行,这样就不会浪费另一个对象所占用的空间
    static class LeftViewHolder extends ViewHolder
    {
        LinearLayout left_layout;
        TextView left_info,left_price,left_cost;
        public LeftViewHolder(View v)
        {
            super(v);
            left_layout = (LinearLayout) v.findViewById(R.id.left_layout);
            left_info = (TextView) v.findViewById(R.id.left_info);
            left_price = (TextView) v.findViewById(R.id.left_price);
            left_cost = (TextView) v.findViewById(R.id.left_cost);
        }
    }
    static class RightViewHolder extends ViewHolder
    {
        LinearLayout right_layout;
        TextView right_info,right_price,right_cost;
        public RightViewHolder(View v)
        {
            super(v);
            right_layout = (LinearLayout) v.findViewById(R.id.right_layout);
            right_info = (TextView) v.findViewById(R.id.right_info);
            right_price = (TextView) v.findViewById(R.id.right_price);
            right_cost = (TextView) v.findViewById(R.id.right_cost);
        }
    }
    public HouseAdapter(List<House> hList)
    {
        houseList = hList;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.house_item,
            parent,false);
        if (viewType == LEFT)
            return new LeftViewHolder(v);
        return new RightViewHolder(v);
    }
    @Override
    public void onBindViewHolder(ViewHolder holder, int position)
    {
        House house = houseList.get(position);
        if (holder instanceof LeftViewHolder)
        {
            LeftViewHolder leftHolder= (LeftViewHolder) holder;
            String[] s = house.getContent();
            leftHolder.left_info.setText(s[0]);
            leftHolder.left_price.setText(s[1]);
            leftHolder.left_cost.setText(s[2]);
        }
        else
        {
            RightViewHolder rightHolder = (RightViewHolder)holder;
            String[] s = house.getContent();
            rightHolder.right_info.setText(s[0]);
            rightHolder.right_price.setText(s[1]);
            rightHolder.right_cost.setText(s[2]);
        }
    }
    @Override
    public int getItemCount()
    {
        return houseList.size();
    }
    //复写返回当前卡片的左右类型
    @Override
    public int getItemViewType(int position)
    {
        return houseList.get(position).getType();
    }
}

其实这里并没有起到省空间的作用,因为我的左右卡片布局写在一起了,这样我每次用LayoutInflater的inflate()方法去加载布局时两个都加载了,其实并没有达到想要的效果,不过我写完之后不想改了,只需要把分布局那个左右卡片分成两个布局来写就行了

主类

package com.example.roompractice;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView houseRecyclerView;
    private List<House> houseList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        HouseAdapter adapter = new HouseAdapter(houseList);
        recyclerView.setAdapter(adapter);
    }
    //卡片初始化
    private void init()
    {
        for (int i = 0;i < 5;i ++)
        {
            House h1 = new House("307平/南北/低楼层","37000/平","1900万",1);
            houseList.add(h1);
            House h2 = new House("204p平/东西/高楼层","29000/平","1300万",0);
            houseList.add(h2);
        }
    }
}

左右卡片的布局分开写是有必要的,因为当我们需要加载很多的卡片时,就会节省一半的空间,这样加载也会快很多

你可能感兴趣的:(安卓学习)