Вот мой код для фрагмента:
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);
prepareUserData();
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(userAdapter);
userAdapter.SetOnItemClickListener(new UserAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
FragmentManager fm = getFragmentManager();
android.app.DialogFragment dialogFragment = new DialogFragment();
dialogFragment.show(fm, "Sample Fragment");
}
});
return view;
}
}
как правильно использовать метод onPrepareData()? выводит только один элемент на экран,а должно 4,не понимаю в чем проблема.
UserAdapter
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.MyViewHolder> {
private List<User> UserList;
OnItemClickListener mItemClickListener;
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,mItemClickListener);
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 implements View.OnClickListener {
public ImageView UserIcon;
public TextView UserTitle;
public TextView UserDescription;
public Button UserFollowButton;
public MyViewHolder(View itemView, UserAdapter.OnItemClickListener clickListener) {
super(itemView);
clickListener = (OnItemClickListener) mItemClickListener;
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);
//itemView.setOnClickListener(this);
UserFollowButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
mItemClickListener.onItemClick(v, getPosition());
}
}
public interface OnItemClickListener{
public void onItemClick(View view,int position);
}
public void SetOnItemClickListener(final OnItemClickListener mItemClickListener) {
this.mItemClickListener = mItemClickListener;
}
}
xm layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.myapplication.Fragment_Username_Search">
<LinearLayout
android:id="@+id/Search_field"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:focusable="true"
android:focusableInTouchMode="true"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_search_black_48dp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:tag="Search"
android:textIsSelectable="false"
/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:layout_below="@id/Search_field"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/RecyclerView"
>
</android.support.v7.widget.RecyclerView>
</RelativeLayout>