android 开发 RecyclerView 横排列列表布局

1.写一个一竖的自定义布局:



    
    
    


预览效果:

android 开发 RecyclerView 横排列列表布局_第1张图片

2.写一个适配器的类

package com.example.lenovo.myrecyclerview.RecyclerViewToolkit;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.lenovo.myrecyclerview.R;

import java.util.List;

/**
 * Created by lenovo on 2018/5/2.
 */

public class ListAdapter extends RecyclerView.Adapter {
    private List mDataList;
    static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView imageAvatar;
        TextView nameText;
        TextView contentsText;
        public ViewHolder(View itemView) {
            super(itemView);
            //注意这里可能需要import com.example.lenovo.myrecyclerview.R; 才能使用R.id
            imageAvatar = (ImageView)itemView.findViewById(R.id.horizontalImageView);
            nameText =(TextView) itemView.findViewById(R.id.horizontalTextViewName);
            contentsText = (TextView)itemView.findViewById(R.id.horizontalTextViewContent);

        }
    }
    public  ListAdapter(List listDatas){
        mDataList = listDatas;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_horizontal_list_view,
                parent,false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ListData listData = mDataList.get(position);
        holder.imageAvatar.setImageResource(listData.getImageView());
        holder.nameText.setText(listData.getNameText());
        holder.contentsText.setText(listData.getContentsText());
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }
}

3.在RecyclerView 横排列列表布局 活动类中,将RecyclerView 排列方式修改成横向。



package com.example.lenovo.myrecyclerview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.example.lenovo.myrecyclerview.RecyclerViewToolkit.ListAdapter;
import com.example.lenovo.myrecyclerview.RecyclerViewToolkit.ListData;

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

public class RecyclerViewActivity extends AppCompatActivity {
    private List listDatas = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view);
        addingData();
        RecyclerView recyclerView = (RecyclerView)findViewById(R.id.RecyclerView);
        //在此处修改布局排列方向
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(layoutManager);
        ListAdapter listAdapter = new ListAdapter(listDatas);
        recyclerView.setAdapter(listAdapter);
    }
    public void addingData(){
        ListData ace = new ListData(R.drawable.ace,"ace", "波特卡斯·D·艾斯:" +
                "烧烧果实的前任能力者,绰号“火拳”,实力强大。");
        listDatas.add(ace);
        ListData arlong = new ListData(R.drawable.arlong,"arlong",
                "恶龙海贼团:”由一群离开王下七武海之一甚平统治的鱼人所组成的海贼团,海贼船名为“鲨鱼˙斯帕布号”," +
                        "船头放置著和恶龙一样的锯齿状鼻子,由于总是到处上岸杀戮,成为恐怖和霸权的象征。");
        listDatas.add(arlong);
        ListData barbe_blanche = new ListData(R.drawable.barbe_blanche,"barbe_blanche",
                "爱德华·纽盖特:外号“白胡子”。“四皇”之一(生前),白胡子海贼团的船长,实力极其强劲,被称为「世界最强男人」。" +
                        "能力是被誉为最强超人系的震震果实,武器为薙刀。");
        listDatas.add(barbe_blanche);
//以下略。。。。。。。。。。


    }

}


运行效果:

android 开发 RecyclerView 横排列列表布局_第2张图片

你可能感兴趣的:(android,android开发)