RecyclerView中的分隔符

时间:2016-03-21 01:20:35

标签: android android-recyclerview

我需要在RecyclerView中使用分割器,如图像,我无法理解如何做到这一点。分割器必须看起来像textview或图像,我需要制作不同的ViewHolder或者什么?我谷歌它并没有找到任何例子看起来像我需要的。 它必须看起来像这样 http://www.picshare.ru/view/7210663/

我的适配器:

package com.example.myapplication;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;
import java.util.Objects;

import butterknife.Bind;

/**
 * Created by Рост on 20.03.2016.
 */
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.MyViewHolder> {
    private List<User> UserList;

    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder viewHolder = null;
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());

                    View viewUser = inflater.inflate(R.layout.row_item_layout,parent,false);
                    viewHolder = new MyViewHolder(viewUser);


        return (MyViewHolder) viewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.UserTitle.setText(UserList.get(position).getTitle());
        holder.UserDescription.setText(UserList.get(position).getDescription());
        holder.UserIcon.setImageResource(R.drawable.emilypic);
    }


    public UserAdapter(List<User> UserList){
    this.UserList = UserList;
}
    @Override
    public int getItemCount() {
        return UserList.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public ImageView UserIcon;
        public TextView UserTitle;
        public TextView UserDescription;
        public Button UserFollowButton;
        public MyViewHolder(View itemView) {
            super(itemView);
            UserIcon = (ImageView) itemView.findViewById(R.id.item_icon);
            UserTitle = (TextView) itemView.findViewById(R.id.item_title);
            UserDescription = (TextView) itemView.findViewById(R.id.item_description);
            UserFollowButton = (Button) itemView.findViewById(R.id.item_follow_button);

        }

    }

}

包含数据的片段:

package com.example.myapplication;

import android.annotation.TargetApi;
import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

import butterknife.Bind;


public class Fragment_Username_Search extends Fragment {
private List<User> userList = new ArrayList<>();
    private RecyclerView recyclerView;
    private UserAdapter userAdapter;

    @TargetApi(Build.VERSION_CODES.M)
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    private void prepareUserData() {
User user = new User(R.drawable.emilypic,"Marry Young","marbear",R.id.item_follow_button);
userList.add(user);
        user = new User(R.drawable.emilypic,"Marry Young","marbear",R.id.item_follow_button);
        userList.add(user);
        user = new User(R.drawable.emilypic,"Sammy Lee","sambam",R.id.item_follow_button);
        userList.add(user);
        user = new User(R.drawable.emilypic,"Lilly Martinson","lilmartini",R.id.item_follow_button);
        userList.add(user);
        userAdapter.notifyDataSetChanged();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

       View view =  inflater.inflate(R.layout.fragment__username__search, container, false);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
        recyclerView = (RecyclerView) view.findViewById(R.id.RecyclerView);
        userAdapter = new UserAdapter(userList);
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(userAdapter);
        prepareUserData();

        return view;
    }


}

0 个答案:

没有答案