Closed gagan50 closed 6 years ago
called onItemInCenter
, the argument is the selected itemView, you can change you selected style in that method
and in onBindViewHolder
you can write like this to set the unselected style
@Override
public void onBindViewHolder(VH holder, int position) {
Object obj = holder.itemView.getTag(R.string.tag_is_center);
boolean selected = false;
if (obj != null)
selected = (Boolean)obj;
if (!selected) {
// the item is not in center, revert the unselected style
}
My Code is below please check it's not working correctly
import android.content.Context; import android.graphics.Color; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast;
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List;
import mac.app.meetandchill.DataString; import mac.app.meetandchill.R; import mac.app.meetandchill.adapter.CircularRecyclerViewAdapter;
/**
email: 723526676@qq.com */ public class MultiModeFragment extends Fragment {
private CircleRecyclerView mCircleRecyclerView; private ItemViewMode mItemViewMode; private LinearLayoutManager mLayoutManager;
private boolean mIsNotLoop;
ArrayList
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment, container, false); }
@Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState);
int modeType = getArguments().getInt("mode_type");
mCircleRecyclerView = (CircleRecyclerView)
view.findViewById(R.id.circle_rv);
switch (modeType) {
case 1:
mItemViewMode = new CircularViewMode();
mLayoutManager = new LinearLayoutManager(getContext());
break;
case 2:
mItemViewMode = new ScaleXViewMode();
mLayoutManager = new LinearLayoutManager(getContext(),
LinearLayoutManager.HORIZONTAL, false); break; case 3: mItemViewMode = new ScaleYViewMode(); mLayoutManager = new LinearLayoutManager(getContext()); break; case 4: mItemViewMode = new RotateXScaleYViewMode(); mLayoutManager = new LinearLayoutManager(getContext()); break; case 5: mItemViewMode = new RotateYScaleXViewMode(); mLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false); break; case 6: mItemViewMode = new CircularViewMode(); mLayoutManager = new LinearLayoutManager(getContext()); mIsNotLoop = true; break; case 7: mItemViewMode = new CircularHorizontalMode(); mLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false); break; case 8: mItemViewMode = new CircularViewRTLMode(); mLayoutManager = new LinearLayoutManager(getContext()); break; case 9: // notice when use horizontal layoutManager and item in recyclerView bottom // the item xml file is not useful set marginTop and set alginParentBottom, // u can use the first argument to control the item position of the recyclerView // and this offset need use some density util to resolve adaptation // and the offset is relative recyclerView top abs distance mItemViewMode = new CircularHorizontalBTTMode(600, false); mLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false); break;
}
mCircleRecyclerView.setLayoutManager(mLayoutManager);
mCircleRecyclerView.setViewMode(mItemViewMode);
mCircleRecyclerView.setNeedCenterForce(true);
mCircleRecyclerView.setNeedLoop(!mIsNotLoop);
mCircleRecyclerView.setOnCenterItemClickListener(new
CircleRecyclerView.OnCenterItemClickListener() { @Override public void onCenterItemClick(View v) { // Toast.makeText(getContext(), "Center Clicked", Toast.LENGTH_SHORT).show(); } });
arrayList.add(0, "Mandarian");
arrayList.add(1, "Spanish");
arrayList.add(2, "English");
arrayList.add(3, "Arabic");
arrayList.add(4, "Portuguese");
arrayList.add(5, "Russian");
arrayList.add(6, "Thai ");
mCircleRecyclerView.setAdapter(new A(arrayList));
mCircleRecyclerView.post(new Runnable() {
@Override
public void run() {
mCircleRecyclerView.scrollBy(0,
-mCircleRecyclerView.getHeight()/2 + mCircleRecyclerView.getChildAt(0).getHeight()/2); } });
mCircleRecyclerView.setCurrentItemCallback(new CurrentItemCallback() {
@Override
public void onItemInCenter(View centerItem) {
selectedItemPosition = (int)
centerItem.getTag(R.string.item_position);
selectedItemPosition = selectedItemPosition % arrayList.size();
Log.e("LENGTH_SHORT",
"onItemInCenter:>>>>>>>"+selectedItemPosition); //Toast.makeText(getActivity(), "" + position, Toast.LENGTH_SHORT).show(); } });
}
class A extends RecyclerView.Adapter
ArrayList<String> stringArrayList;
public A(ArrayList<String> list) {
this.stringArrayList = list;
}
@Override
public VH onCreateViewHolder(ViewGroup parent, int viewType) {
VH h = null;
if (mCircleRecyclerView.getLayoutManager().canScrollHorizontally())
{ h = new VH(LayoutInflater.from(getContext()) .inflate(R.layout.item_h, parent, false)); } else if (mCircleRecyclerView.getLayoutManager().canScrollVertically()) { if (mItemViewMode instanceof CircularViewMode) h = new VH(LayoutInflater.from(getContext()) .inflate(R.layout.item_c_v, parent, false)); else if (mItemViewMode instanceof CircularViewRTLMode) h = new VH(LayoutInflater.from(getContext()) .inflate(R.layout.item_c_rtl_v, parent, false)); else if (mItemViewMode instanceof CircularHorizontalBTTMode) { h = new VH(LayoutInflater.from(getContext()) .inflate(R.layout.item_h_btt, parent, false)); } else h = new VH(LayoutInflater.from(getContext()) .inflate(R.layout.item_v, parent, false)); }
return h;
}
@Override
public void onBindViewHolder(VH holder, int position) {
holder.tv.setText("" + stringArrayList.get(position %
stringArrayList.size()));
Object obj = holder.itemView.getTag(R.string.tag_is_center);
boolean selected = false;
if (obj != null)
selected = (Boolean)obj;
if (!selected) {
holder.tv.setTextColor(Color.BLACK);
// the item is not in center, revert the unselected style
}else {
holder.tv.setTextColor(Color.BLUE);
}
/* Glide.with(getContext())
.load(mImgList.get(position % mImgList.size()))
.bitmapTransform(new CropCircleTransformation(getContext()))
.into(holder.iv);*/
//endregion
// when the other item click, it can scroll to center
/* holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCircleRecyclerView.smoothScrollToView(v);
}
});*/
holder.itemView.setTag(R.string.item_position, position);
}
@Override
public int getItemCount() {
return mIsNotLoop ? stringArrayList.size() : Integer.MAX_VALUE;
}
}
class VH extends RecyclerView.ViewHolder {
TextView tv;
ImageView iv;
public VH(View itemView) {
super(itemView);
tv = (TextView) itemView.findViewById(R.id.item_text);
iv = (ImageView) itemView.findViewById(R.id.item_img);
}
} }
Thanks alot that's working cool
Gagandeep Singh (Asso. Android Developer) Emp Id- 9677
On Thu, Jul 5, 2018 at 3:16 PM, Matt Yao notifications@github.com wrote:
watch this commit https://github.com/kHRYSTAL/CircleRecyclerView/commit/c59408fae76e39d818278aae321d479ab0df697e
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/kHRYSTAL/CircleRecyclerView/issues/25#issuecomment-402667860, or mute the thread https://github.com/notifications/unsubscribe-auth/AfxG8InCufIRu7qKLOlkbUgb2HMnvBf-ks5uDeBtgaJpZM4VDeAf .
Once item position has been saved then everytime how i can show that item selected? Please provide source code for it.
Thank you