post1
Using RecyclerView :
Dependency : compile 'com.android.support:recyclerview-v7:27.0.1'
Dependency : compile 'com.android.support:recyclerview-v7:27.0.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'compile 'com.jakewharton:butterknife:8.8.1'
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.app.mgu.recyclerview.MainActivity">
<android.support.v7.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent" android:text="Hello World!" android:background="@color/colorPrimary"/>
</android.support.constraint.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:focusable="true" android:paddingLeft="16dp" android:paddingRight="16dp" android:paddingTop="10dp" android:paddingBottom="10dp" android:clickable="true" android:orientation="vertical">
<TextView android:id="@+id/txtView" android:textSize="16dp" android:textStyle="bold" android:text="sdlfioi" android:layout_width="wrap_content" android:layout_alignParentStart="true" android:layout_alignParentLeft="true" android:layout_height="wrap_content"/>
<CheckBox android:id="@+id/chkBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/txtView" android:checked="true"/>
</RelativeLayout>
public class MainActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final RecyclerView.Adapter adapter = new AdapterData();
LinearLayoutManager l = new LinearLayoutManager(this);
l.setOrientation(LinearLayoutManager.VERTICAL);
((RecyclerView) findViewById(R.id.recyclerView)).setAdapter(adapter);
((RecyclerView) findViewById(R.id.recyclerView)).setLayoutManager(l);
// ((RecyclerView) findViewById(R.id.recyclerView)).addOnItemTouchListener(new Recyc);
((RecyclerView) findViewById(R.id.recyclerView)).addOnItemTouchListener(new RecyclerItemClickListener(this, ((RecyclerView) findViewById(R.id.recyclerView)), new RecyclerItemClickListener.OnItemClickListener() {
@Override public void onItemClick(View view, int position) {
// ...
Toast.makeText(getBaseContext(), "OnItem CLick" , Toast.LENGTH_LONG).show();
}
@Override public void onItemLongClick(View view, int position) {
// ...
Toast.makeText(getBaseContext(), "OnItem Long.. CLick" , Toast.LENGTH_LONG).show();
}
}));
}
}
public class AdapterData extends RecyclerView.Adapter<AdapterData.ViewHolder> {
@Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View inflater = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_child, parent, false);
return new ViewHolder(inflater);
}
MOdel m ;
@Override public void onBindViewHolder(ViewHolder holder, int position) {
System.out.println("BindViewHolder");
holder.txtView.setText("Temp..");
// holder.chkBox.setOnCheckedChangeListener(new a());
m = listModel.get(position);
holder.chkBox.setChecked(m.isChhk);
holder.chkBox.setOnCheckedChangeListener(new a(position));
}
@Override public int getItemCount() {
return 100;
}
// List<Boolean> listChecked = new ArrayList<Boolean>(); List<MOdel> listModel = new ArrayList<MOdel>();
public AdapterData() {
for(int i = 0 ; i < 100 ; i++) {
MOdel m = new MOdel();
m.isChhk = false;
listModel.add(m);
}
}
public class a implements CheckBox.OnCheckedChangeListener {
int position;
a(int posi) {
this.position = posi;
}
@Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
listModel.get(position).isChhk = !isChecked;
}
}
class ViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.txtView)
TextView txtView;
@BindView(R.id.chkBox)
CheckBox chkBox;
public ViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
}
public class MOdel {
public boolean isChhk = true;
}
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
public interface OnItemClickListener {
void onItemClick(View view, int position);
void onItemLongClick(View view, int position);
}
private OnItemClickListener mListener;
private GestureDetector mGestureDetector;
public RecyclerItemClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener) {
mListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override public boolean onSingleTapUp(MotionEvent e) {
return true;
}
@Override public void onLongPress(MotionEvent e) {
View childView = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null) {
mListener.onItemLongClick(childView, recyclerView.getChildAdapterPosition(childView));
}
}
});
}
@Override public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
View childView = view.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
}
return false;
}
@Override public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
}
@Override public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
Comments
Post a Comment