RecycleView의 전체 선택 및 반전
12844 단어 이동
1.activity_xml 코드:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/all"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=" " />
<Button
android:id="@+id/fan"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=" " />
LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent">
android.support.v7.widget.RecyclerView>
2.item
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/text_item"
android:layout_width="300dp"
android:layout_height="wrap_content"
/>
<CheckBox
android:id="@+id/mCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
LinearLayout>
<View
android:id="@+id/v"
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#C0C0C0" />
3. Mainactivity의 코드:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button all;
private Button fan;
private RecyclerView rv;
private ArrayList arr;
private LinearLayoutManager linearLayoutManager;
private MyRecycleVAdapter myadapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
private void initData() {
arr = new ArrayList<>();
for (int i = 0; i < 100; i++) {
Bean bean = new Bean(i + "", false);
arr.add(bean);
}
linearLayoutManager = new LinearLayoutManager(this);
rv.setLayoutManager(linearLayoutManager);
// rv.addItemDecoration(new drawline());
//
if (myadapter == null) {
myadapter = new MyRecycleVAdapter(arr, MainActivity.this);
rv.setAdapter(myadapter);
} else {
myadapter.notifyDataSetChanged();
}
this.myadapter.setData(arr);
}
private void initView() {
all = (Button) findViewById(R.id.all);
fan = (Button) findViewById(R.id.fan);
rv = (RecyclerView) findViewById(R.id.rv);
all.setOnClickListener(this);
fan.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.all:
myadapter.setELL();
break;
//
case R.id.fan:
myadapter.setfalse();
break;
}
}
4. Myadapter의 코드:
public class MyRecycleVAdapter extends RecyclerView.Adapter {
private ArrayList list = new ArrayList<>();
private Context context;
public MyRecycleVAdapter(ArrayList list, Context context) {
this.list = list;
this.context = context;
}
@Override
public MyViewHoder onCreateViewHolder(ViewGroup parent, int viewType) {
View inflate = View.inflate(context, R.layout.item, null);
MyViewHoder myViewHoder = new MyViewHoder(inflate);
return myViewHoder;
}
@Override
public void onBindViewHolder(final MyViewHoder holder, final int position) {
holder.text_item.setText(list.get(position).getStr());
holder.mCheckBox.setChecked(list.get(position).istrue());
holder.text_item.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "" + list.get(position).toString(), Toast.LENGTH_SHORT).show();
}
});
holder.text_item.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(" " + position);
builder.show();
return false;
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public void setData(ArrayList arr) {
if (arr != null) {
list.addAll(arr);
}
}
public void setELL() {
for (int i = 0; i < list.size(); i++) {
Bean bean = list.get(i);
bean.setIstrue(true);
}
notifyDataSetChanged();
}
public void setfalse() {
for (int i = 0; i < list.size(); i++) {
Bean bean = list.get(i);
bean.setIstrue(false);
}
notifyDataSetChanged();
}
class MyViewHoder extends RecyclerView.ViewHolder {
private final TextView text_item;
private final CheckBox mCheckBox;
private final View viewById;
public MyViewHoder(View itemView) {
super(itemView);
text_item = (TextView) itemView.findViewById(R.id.text_item);
mCheckBox = (CheckBox) itemView.findViewById(R.id.mCheckBox);
viewById = itemView.findViewById(R.id.v);
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
js 마우스 이동 방향을 판단하는 방법PC 단말기의 마우스를 옮겨 옮기는 효과가 매우 좋다. 여기서 마우스를 옮겨 옮기는 방법을 판단하고 틈이 나면 효과가 있는 글을 보내지만 물고기를 주는 것보다 물고기를 잡는 것이 낫다. 이 방법이 있는데 효과가 so...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.