如何在Android中的创建列表中使用RecyclerView?

android android-studio android-recyclerview adapter android-viewholder

1071 观看

3回复

1 作者的声誉

这个问题已经在这里有了答案:

我想使用recyclerView创建列表,请建议我如何在android中进行操作?

 public class MainActivity extends AppCompatActivity {

        RecyclerView myRecyclerView;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            myRecyclerView = (RecyclerView)findViewById(R.id.my_recycler_view);

            LinearLayoutManager layoutManager = new LinearLayoutManager(this);
            myRecyclerView.setLayoutManager(layoutManager);

            myRecyclerView.setAdapter(new FriendListAdapter());

        }
    }
作者: user7553631 的来源 发布者: 2017 年 2 月 14 日

回应 3


0

2803 作者的声誉

XML:

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_test"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scrollbarStyle="outsideInset"
        android:scrollbars="vertical" />

JAVA类

    ArrayList<String>list_friend;
    RecyclerView rv_test = (RecyclerView) view.findViewById(R.id.rv_test);
    LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(YourActivity.this);
    rv_test.setLayoutManager(mLinearLayoutManager);
    rv_test.setHasFixedSize(true);
    rv_test.setItemAnimator(new DefaultItemAnimator());

    //Set Adapter

    FriendListAdapter mAdapter = new FriendListAdapter(list_friend);
    rv_test.setAdapter(mAdapter);

FriendListAdapter

    public class FriendListAdapter extends RecyclerView.Adapter<FriendList.ViewWrapper> {
        ArrayList<String>list_friend;
        public FriendList( ArrayList<String>list_friend) {
           this.list_friend=list_friend;
        }

        @Override
        public ViewWrapper onCreateViewHolder(ViewGroup viewGroup, int i) {
            // create a new view
            @SuppressLint("InflateParams") View itemLayoutView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_friend, null);
            // create ViewHolder
            return new ViewWrapper(itemLayoutView);
        }

        @Override
        public void onBindViewHolder(final ViewWrapper viewWrapper, int position) {
            viewWrapper.getName().setText(list_friend.get(viewWrapper.getAdapterPosition()));
        }

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

        class ViewWrapper extends RecyclerView.ViewHolder {
            final View base;
            TextView tv_name;

            public ViewWrapper(View itemView) {
                super(itemView);
                base = itemView;
            }

            TextView getName() {
                if (tv_name == null) {
                    tv_name = (TextView) base.findViewById(R.id.tv_name);
                }
                return (tv_name);
            }
        }
    }
作者: user2025187 发布者: 2017 年 2 月 14 日

0

267 作者的声誉

MyAdapter适配器=新的MyAdapter(MainActivity.this,您的列表)recyclerview.setAdapter(adapter);

然后创建一个适配器类

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.CustomViewHolder> implements View.OnClickListener {

    ArrayList<String> arr;
    Context mContext;

    public  MyAdapter(Context context, ArrayList<String> Arr,) {
        this.arr = arr;
        this.mContext = context;

    }

    @Override
    public int getItemCount() {
        // TODO Auto-generated method stub
        return (null != arr ? arr.size() : 0);
    }

    @Override
    public void onBindViewHolder(final CustomViewHolder arg0, final int position) {


    }


    @Override
    public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int arg1) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item, null);
        RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(lp);


        CustomViewHolder viewHolder = new CustomViewHolder(view);

        return viewHolder;

    }

    @Override
    public void onClick(View v) {



    }

    public class CustomViewHolder extends RecyclerView.ViewHolder {


        public CustomViewHolder(View rowView) {
            super(rowView);


        }
    }



}
作者: Rajkumar Kumawat 发布者: 2017 年 2 月 14 日

0

757 作者的声誉

XML档案-

<android.support.v7.widget.RecyclerView
        android:id="@+id/recycle_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

在主要活动中:-

  myTagRecyclerView = (RecyclerView) view.findViewById(R.id.cardList);
  RecyclerView.LayoutManager mLayoutManager = newLinearLayoutManager(this);
   myTagRecyclerView.setLayoutManager(mLayoutManager);
   myTagRecyclerView.setItemAnimator(new DefaultItemAnimator());
MyTagRecyclerViewAdapter adapterTagItemList = new MyTagRecyclerViewAdapter(this, dataList);
myTagRecyclerView.setAdapter(adapterTagItemList);

在适配器中:

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.tine.tine.R;

import java.util.List;

/**
 * Created by Nishchal on 2/14/2017.
 */
public class TestRecycleView extends RecyclerView.Adapter<TestRecycleView.ViewHolder> {
    private final Context mContext;
    private List<TestModel> mTestModelList;
    private String TAG = TestRecycleView.class.getSimpleName();
    private OnItemClickListener onItemClickListener;


    public TestRecycleView(Context mContext, List<TestModel> mTestModelList) {
        this.mTestModelList = mTestModelList;
        this.mContext = mContext;
    }

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

        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        TestModel mTestModel = mTestModelList.get(position);
        //TODO Fill in your logic for binding the view.
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getItemCount() {
        if (mTestModelList == null) {
            return 0;
        }
        return mTestModelList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        public final TextView venuename_txtview = null;

        public ViewHolder(View itemView) {
            super(itemView);
//            venuename_txtview = (TextView) itemView.findViewById(R.id.text);

            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            try {

                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(v, getAdapterPosition());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public interface OnItemClickListener {
        public void onItemClick(View view, int position);
    }

}
作者: Nishchal 发布者: 2017 年 2 月 14 日
32x32