Android recyclerview selects the implementation code of multiple items
•
Android
Imitate the channel selection of Netease News client's reading preference. First look at the implemented page:
Direct code:
import android.content.res.Resources; import android.content.res.TypedArray; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.DefaultItemAnimator; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.helper.itemtouchhelper; import android.util.Log; import android.util.SparseBooleanArray; import android.view.View; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class RecyclerViewActivity extends AppCompatActivity { private RecyclerView recycler; private RecyclerAdapter mAdapter; private List<PreferCustomizableChannel> channels = new ArrayList<>(); private List<PreferCustomizableChannel> channelsSelected; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_recycler_view_acitivity); initData(); initUI(); findViewById(R.id.resultBTN).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int lens = channelsSelected.size(); for (int i =0 ; i < lens; i++) { PreferCustomizableChannel customizableChannel = channelsSelected.get(i); if (customizableChannel.isSelected()) { Log.i("RecyclerViewActivity","onClick: "+customizableChannel.toString()); } } } }); } private void initData() { Resources resources = getResources(); TypedArray array = resources.obtainTypedArray(R.array.prefer_channel_icon); int len = array.length(); String[] name = resources.getStringArray(R.array.prefer_channel_name); for (int i = 0; i < len; i++) { PreferCustomizableChannel customizableChannel = new PreferCustomizableChannel(); customizableChannel.setChannel(name[i]); customizableChannel.setResId(array.getResourceId(i,0)); customizableChannel.setSelected(false); customizableChannel.setId(i * 100); channels.add(customizableChannel); } array.recycle(); channelsSelected = channels; } private void initUI() { recycler = (RecyclerView) findViewById(R.id.recycler); final GridLayoutManager manager = new GridLayoutManager(this,3); recycler.setLayoutManager(manager); recycler.setHasFixedSize(true); recycler.setItemAnimator(new DefaultItemAnimator()); mAdapter = new RecyclerAdapter(RecyclerViewActivity.this,channels); recycler.setAdapter(mAdapter); mAdapter.setClickListener(new OnRecyclerViewItemClickListener() { @Override public void onItemClick(View view,int position) { SparseBooleanArray selecteds = mAdapter.getSelectedItem(); int len = channels.size(); for (int i = 0; i < len; i++) { if (selecteds.get(i)) { channelsSelected.get(position).setSelected(true); } } } }); } }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码