-3

私はrecyclerViewを使用してリストを作成したいと思いますか?アンドロイドでそれを行う方法を教えてください。RecyclerViewを使ってアンドロイドでリストを作成するには?

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()); 

     } 
    } 
+0

[RecyclerView Documentation](http://stackoverflow.com/documentation/android/169/recyclerview#t=201702140912313297647) –

答えて

0

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); 
      } 
     } 
    } 
0

MyAdapterアダプタ=新しいMyAdapter(MainActivity.this、あなたのリスト) recyclerview.setA dapter(アダプター);その後、

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); 


     } 
    } 



} 
0

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); 
    } 

} 
関連する問題