0

英語を私に許してください。各項目のチェックボックスをクリックして次の項目にスクロールする方法(recyclerview)

私のリストには10​​0項目があります。各項目は2つのチェックボックスで構成され、チェックボックスをクリックするたびに次の項目にスクロールします。

この質問はすでに

に答えてきた場合、これは、これはあなたがそれを行うことができます主な活動

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

    final RecyclerView recyclerView = (RecyclerView) 
    findViewById(R.id.recyclerView_test); 
    final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext()); 
    recyclerView.setLayoutManager(linearLayoutManager); 

    final CustomAdapter customAdapter = new CustomAdapter(mainActivity.this, answerFirst,answerSecond); 
    recyclerView.setAdapter(customAdapter); 
} 
+0

なぜあなたは 'getApplicationContext()'を使用していますか?これを使わないで、 'Contexts'の型に違いがあります。アプリケーションコンテキストを使用するとメモリリークが発生する可能性があります。これは、アプリケーションコンテキストが他のものよりも長く滞在し、 'Activity'を破壊できなくなり、メモリの問題を引き起こす可能性があるからです。 – Sakiboy

+0

@Sakiboyあなたのアドバイスありがとうございます – moslem

答えて

1

ある

public class CustomAdapter extends 
     RecyclerView.Adapter<CustomAdapter.MyViewHolder> { 

private ArrayList answerFirst; 
private ArrayList answerSecond; 
private Context context; 

public CustomAdapter(Context context, ArrayList questionList, ArrayList 
answerFirst, ArrayList answerSecond) { 
    this.context = context; 
    this.answerFirst = answerFirst; 
    this.answerSecond = answerSecond; 
} 

@Override 
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View v = 
    LayoutInflater.from(parent.getContext()).inflate(R.layout.test_items, 
    parent, false); 
    return new MyViewHolder(v); 
} 

@Override 
public void onBindViewHolder(final MyViewHolder holder, final int position) 
    { 

     holder.firstanswer.setText(answerFirst.get(position).toString()); 
     holder.secondanswer.setText(answerSecond.get(position).toString()); 
    } 

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

public class MyViewHolder extends RecyclerView.ViewHolder { 
    TextView question; 
    CheckBox firstanswer; 
    CheckBox secondanswer; 
    public MyViewHolder(View itemView) { 
     super(itemView); 
     question = (TextView) itemView.findViewById(R.id.question_text); 
     firstanswer = (CheckBox) itemView.findViewById(R.id.first_answer); 
     secondanswer = (CheckBox) itemView.findViewById(R.id.second_answer); 
      } 
     } 
    } 

私のカスタムアダプタです私に最良の答えのリンクをお願いしメインクラスの中のアダプタクラスをチェックボックスのリスナメソッドにすることによって、RecyclerViewがグローバル変数であるため、それにアクセスすることができるので、必要な位置にスクロールします必要な位置にスクロールします。他

rv.scrollToPosition(youPositionInTheAdapter) 

コンストラクタでRecyclerViewのインスタンスを渡すと同じ

を行うと、getAdapterPosition()

により、アダプタ内の現在のアイテムの位置を取得するか、このようなレイアウトマネージャでそれを行うことができます

rv.getLayoutManager().scrollToPosition(youPositionInTheAdapter) 
+0

ありがとうございますsagar – moslem

関連する問題