earl cabanig
earl cabanig

Reputation: 53

adding RecyclerView in a fragment

I dont know if my title is correct but I hope you can get my question with the help of some pictures. So I have a project the will display a fragment containing book info such us Title, Author, Publisher etc, at the same time I want to display comments about the book I mean once it loads. It also loads a recyclerview containing the comments about the books like in google play store where you may see the comments of different users. How can I achieve this? I do know how to create a recyclerview. here is the image for more clarity. this is the image

Upvotes: 0

Views: 46

Answers (1)

swetabh suman
swetabh suman

Reputation: 1989

For RecyclerView you need to add

compile 'com.android.support:recyclerview-v7:24.2.1'

in your dependencies. Inside module level build.gradle file. After adding the compilation file , sync your project. Then inside your layout add the recyclerView. Like this

<android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_funds"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:listitem="@layout/layout_funds_item" />

here the attribute "listitem" will show how your recyclerview will be seen when items will be inflated.

Now in your activity or fragment initialize the recyclerview.

    private RecyclerView mRecyclerView;
    mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_funds);

After that you need to set the layoutManger for your recyclerview. A LayoutManager is responsible for measuring and positioning item views within a RecyclerView as well as determining the policy for when to recycle item views that are no longer visible to the user. By changing the LayoutManager a RecyclerView can be used to implement a standard vertically scrolling list, a uniform grid, staggered grids, horizontally scrolling collections and more. Several stock layout managers are provided for general use.

mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

After that set your adapter to the recyclerview.

 mRecyclerView.setAdapter(new FundsAdapter(mContext));

Here is the code sample how this FundsAdapter looks like.

public class FundsAdapter extends RecyclerView.Adapter<FundsAdapter.ItemHolder> {
    private Context mContext;

    public FundsAdapter(Context context) {
        mContext = context;
    }

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

    @Override
    public void onBindViewHolder(ItemHolder holder, int position) {
    }

    @Override
    public int getItemCount() {
        return 10;
    }

    public class ItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView vh_Title;
        private TextView vh_FundName;

        public ItemHolder(View itemView) {
            super(itemView);
            vh_Title = (TextView) itemView.findViewById(R.id.txt_fund_bank);
            vh_FundName = (TextView) itemView.findViewById(R.id.txt_fund_name);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            mContext.startActivity(new Intent(mContext, FundsDetailActivity.class));
        }
    }
}

Hope you got your solution. :)

Upvotes: 1

Related Questions