How can I give the viewpager a feature that will never run out and be scrolling indefinitely?
xml fragment_home:
<RelativeLayout
android:id="#+id/page_home_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layoutDirection="rtl"
android:background="#fff"
tools:context=".Fragment.Home.HomeFragment">
<androidx.cardview.widget.CardView
android:id="#+id/card_viewpager"
android:elevation="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
app:cardCornerRadius="5dp">
<ir.develop.mhp.Action.CustomViewPager.ViewPagerCustomDuration
android:layout_width="match_parent"
android:layout_height="235dp"
android:id="#+id/slider_viewpager"
/>
</androidx.cardview.widget.CardView>
<com.tbuonomo.viewpagerdotsindicator.DotsIndicator
android:id="#+id/indicator"
android:layout_below="#id/card_viewpager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:dotsColor="#C5C5C5"
app:dotsCornerRadius="8dp"
android:layout_margin="5dp"
app:dotsSize="7dp"
app:dotsSpacing="4dp"
android:layout_centerHorizontal="true"
android:layoutDirection="ltr"
app:dotsWidthFactor="2.5"
app:selectedDotColor="#color/colorPrimary"
app:progressMode="false"
/>
</RelativeLayout>
xml item_image_slider:
<ImageView android:layout_width="match_parent"
android:layout_height="235dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/image_slider"
android:layout_margin="10dp"
android:scaleType="fitXY"
android:src="#color/colorPrimary"
xmlns:android="http://schemas.android.com/apk/res/android">
java HomeFragment:
#Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_home, null);
cast(view);
setupSlider();
setupRecyclerView();
viewPager.setScrollDurationFactor(3);
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
if (state == viewPager.SCROLL_STATE_IDLE){
userScrolled = false;
}else {
userScrolled = true;
}
}
});
viewPager.setPageTransformer(false, new ViewPager.PageTransformer() {
#Override
public void transformPage(#NonNull View page, float position) {
}
});
return view;
}
private void cast(View view) {
viewPager = view.findViewById(R.id.slider_viewpager);
indicator = view.findViewById(R.id.indicator);
}
private void setupSlider() {
url = new ArrayList<>();
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, Config.host + "getSlide.php", null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("slide");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jo = jsonArray.getJSONObject(i);
url.add(jo.getString("slide"));
}
sliderAdapter = new SliderAdapter(getActivity(), url);
viewPager.setAdapter(sliderAdapter);
startAutoSlider(jsonArray.length());
indicator.setViewPager(viewPager);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
CustomToast.toastError(getActivity());
}
});
RequestVolley.get(getActivity()).add(jsonObjectRequest);
}
private void startAutoSlider(final int len) {
final Handler handler = new Handler();
final Runnable Update = new Runnable() {
public void run() {
if (!userScrolled) {
if (viewPager.getCurrentItem() == len - 1) {
currentPage = 0;
}
viewPager.setCurrentItem(currentPage++, true);
}
}
};
timer = new Timer(); // This will create a new Thread
timer.schedule(new TimerTask() { // task to be scheduled
#Override
public void run() {
handler.post(Update);
}
}, DELAY_MS, PERIOD_MS);
}
java SliderAdapter:
public class SliderAdapter extends PagerAdapter {
private Context context;
private List<String> url;
public SliderAdapter(Context context, List<String> url) {
this.context = context;
this.url = url;
}
#Override
public int getCount() {
return url.size();
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object object) {
return view == object;
}
#NonNull
#Override
public Object instantiateItem(#NonNull ViewGroup container, int position) {
View view = LayoutInflater.from(context).inflate(R.layout.item_image_slider , container , false);
ImageView imageView = view.findViewById(R.id.image_slider);
LoadImage.load(context , url.get(position) , imageView);
container.addView(view);
return view;
}
#Override
public void destroyItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
container.removeView((View) object);
}
}
It's very important not to use libraries.
Related
FRAGMENT CODE
public class MyMallFragment extends Fragment {
public MyMallFragment() {
}
public static SwipeRefreshLayout swipeRefreshLayout;
private RecyclerView homepageRecyclerview;
private MyMallAdapter myMallAdapter;
private ImageView noInternet;
private Button retryBtn;
private List<MyMallModel> myMallModelFakeList = new ArrayList<>();
NetworkInfo networkInfo;
ConnectivityManager connectivityManager;
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_my_mall, container, false);
swipeRefreshLayout = view.findViewById(R.id.refresh_layout);
noInternet = view.findViewById(R.id.no_internet);
retryBtn = view.findViewById(R.id.retry_btn);
homepageRecyclerview = view.findViewById(R.id.my_mall_recyclerview);
swipeRefreshLayout.setColorSchemeColors(getContext().getResources().getColor(R.color.colorPrimary), getContext().getResources().getColor(R.color.colorPrimary), getContext().getResources().getColor(R.color.colorPrimary));
LinearLayoutManager testingLayoutManager = new LinearLayoutManager(getContext());
testingLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
homepageRecyclerview.setLayoutManager(testingLayoutManager);
////////home page fake list
List<SliderModel> sliderModelFakeList = new ArrayList<>();
sliderModelFakeList.add(new SliderModel("null", "#dfdfdf"));
sliderModelFakeList.add(new SliderModel("null", "#dfdfdf"));
sliderModelFakeList.add(new SliderModel("null", "#dfdfdf"));
sliderModelFakeList.add(new SliderModel("null", "#dfdfdf"));
sliderModelFakeList.add(new SliderModel("null", "#dfdfdf"));
List<HorizontalProductScrollModel> horizontalProductScrollModelFakeList = new ArrayList<>();
horizontalProductScrollModelFakeList.add(new HorizontalProductScrollModel("", "", "", "", ""));
horizontalProductScrollModelFakeList.add(new HorizontalProductScrollModel("", "", "", "", ""));
horizontalProductScrollModelFakeList.add(new HorizontalProductScrollModel("", "", "", "", ""));
horizontalProductScrollModelFakeList.add(new HorizontalProductScrollModel("", "", "", "", ""));
horizontalProductScrollModelFakeList.add(new HorizontalProductScrollModel("", "", "", "", ""));
horizontalProductScrollModelFakeList.add(new HorizontalProductScrollModel("", "", "", "", ""));
horizontalProductScrollModelFakeList.add(new HorizontalProductScrollModel("", "", "", "", ""));
myMallModelFakeList.add(new MyMallModel(0, sliderModelFakeList));
myMallModelFakeList.add(new MyMallModel(1, "", "#dfdfdf"));
myMallModelFakeList.add(new MyMallModel(2, "", "#dfdfdf", horizontalProductScrollModelFakeList, new ArrayList<WishlistModel>()));
myMallModelFakeList.add(new MyMallModel(3, "", "#dfdfdf", horizontalProductScrollModelFakeList));
////////home page fake list
myMallAdapter = new MyMallAdapter(myMallModelFakeList);
connectivityManager = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected() == true) {
MainActivity.drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
noInternet.setVisibility(View.GONE);
retryBtn.setVisibility(View.GONE);
homepageRecyclerview.setVisibility(View.VISIBLE);
if (lists.size() == 0) {
lists.add(new ArrayList<MyMallModel>());
myMallAdapter = new MyMallAdapter(lists.get(0));
loadFragmentData(homepageRecyclerview, getContext(), 0, "Home");
} else {
myMallAdapter = new MyMallAdapter(lists.get(0));
myMallAdapter.notifyDataSetChanged();
}
homepageRecyclerview.setAdapter(myMallAdapter);
} else {
MainActivity.drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
homepageRecyclerview.setVisibility(View.GONE);
Glide.with(this).load(R.drawable.no_internet).into(noInternet);
noInternet.setVisibility(View.VISIBLE);
retryBtn.setVisibility(View.VISIBLE);
}
///refresh layout
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
swipeRefreshLayout.setRefreshing(true);
reloadPage();
}
});
///refresh layout
retryBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
reloadPage();
}
});
return view;
}
private void reloadPage() {
networkInfo = connectivityManager.getActiveNetworkInfo();
categoryModelList.clear();
lists.clear();
loadedCategoriesNames.clear();
DBquerries.clearData();
if (networkInfo != null && networkInfo.isConnected() == true) {
MainActivity.drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
noInternet.setVisibility(View.GONE);
retryBtn.setVisibility(View.GONE);
homepageRecyclerview.setVisibility(View.VISIBLE);
myMallAdapter = new MyMallAdapter(myMallModelFakeList);
homepageRecyclerview.setAdapter(myMallAdapter);
loadedCategoriesNames.add("HOME");
lists.add(new ArrayList<MyMallModel>());
loadFragmentData(homepageRecyclerview, getContext(), 0, "Home");
} else {
MainActivity.drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
Toast.makeText(getContext(), "No internet Connction!", Toast.LENGTH_SHORT).show();
/// categoryRecyclerView.setVisibility(View.GONE);
homepageRecyclerview.setVisibility(View.INVISIBLE);
Glide.with(getContext()).load(R.drawable.no_internet).into(noInternet);
noInternet.setVisibility(View.VISIBLE);
retryBtn.setVisibility(View.VISIBLE);
swipeRefreshLayout.setRefreshing(false);
}
}
#Override
public void onStart() {
super.onStart();
}
I tried to add and set adaptor in on start method like it worked the content loaded up while opening but as soon as i scrolled down after the recycler view started to repeat the content so the content loaded 2 times.
XML FILE
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/category_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorAccent"
android:elevation="3dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.454"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:visibility="gone" />
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/refresh_layout"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/category_recycler_view">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/my_mall_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<ImageView
android:id="#+id/no_internet"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_gravity="center"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/retry_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Retry"
android:textSize="16sp"
android:textAllCaps="false"
android:backgroundTint="#color/black"
android:stateListAnimator="#null"
android:textColor="#color/colorAccent"
android:translationZ="8dp"
android:background="#drawable/slider_background"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/category_recycler_view"
app:layout_constraintVertical_bias="0.75" />
</androidx.constraintlayout.widget.ConstraintLayout>
adapter
public class MyMallAdapter extends RecyclerView.Adapter {
private List<MyMallModel> myMallModelList;
private RecyclerView.RecycledViewPool recycledViewPool;
private FirebaseFirestore firebaseFirestore = FirebaseFirestore.getInstance();
private int lastPos=-1;
public MyMallAdapter(List<MyMallModel> myMallModelList) {
this.myMallModelList = myMallModelList;
recycledViewPool=new RecyclerView.RecycledViewPool();
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemViewType(int position) {
switch (myMallModelList.get(position).getType()){
case 0:
return MyMallModel.BANNER_SLIDER;
case 1:
return MyMallModel.STRIP_AD_BANNER;
case 2:
return MyMallModel.HORIZONTAL_PRODUCT_VIEW;
case 3:
return MyMallModel.GRID_PRODUCT_VIEW;
default:
return -1;
}
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
switch (viewType){
case MyMallModel.BANNER_SLIDER:
View BannerSliderview= LayoutInflater.from(parent.getContext()).inflate(R.layout.sliding_ad_layout,parent,false);
return new BannerSliderViewHolder(BannerSliderview);
case MyMallModel.STRIP_AD_BANNER:
View StripAdview= LayoutInflater.from(parent.getContext()).inflate(R.layout.strip_ad_layout,parent,false);
return new StripAdBannerViewHolder(StripAdview);
case MyMallModel.HORIZONTAL_PRODUCT_VIEW:
View horizontalproductview= LayoutInflater.from(parent.getContext()).inflate(R.layout.horizontal_scoll_layout,parent,false);
return new HorizontalProductViewHolder(horizontalproductview);
case MyMallModel.GRID_PRODUCT_VIEW:
View gridproductview= LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_product_layout,parent,false);
return new GridProductViewHolder(gridproductview);
default:
return null;
}
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
switch (myMallModelList.get(position).getType()){
case MyMallModel.BANNER_SLIDER:
List<SliderModel> sliderModelList=myMallModelList.get(position).getSliderModelList();
((BannerSliderViewHolder) holder).setBannerSliderViewPager(sliderModelList);
break;
case MyMallModel.STRIP_AD_BANNER:
String resource=myMallModelList.get(position).getResource();
String color=myMallModelList.get(position).getBackgroundColor();
((StripAdBannerViewHolder)holder).StripAd(resource,color);
break;
case MyMallModel.HORIZONTAL_PRODUCT_VIEW:
String title=myMallModelList.get(position).getTitle();
String colorr=myMallModelList.get(position).getBackgroundColor();
List<HorizontalProductScrollModel> horizontalProductScrollModelList=myMallModelList.get(position).getHorizontalProductScrollModelList();
List<WishlistModel> viewAllProductList=myMallModelList.get(position).getViewAllProductList();
((HorizontalProductViewHolder) holder).setHorizontalProductLayout(horizontalProductScrollModelList,title,colorr,viewAllProductList);
break;
case MyMallModel.GRID_PRODUCT_VIEW:
String gridcolorr=myMallModelList.get(position).getBackgroundColor();
String gridtitle=myMallModelList.get(position).getTitle();
List<HorizontalProductScrollModel> gridProductScrollModelList=myMallModelList.get(position).getHorizontalProductScrollModelList();
((GridProductViewHolder) holder).setGridProductLayout(gridProductScrollModelList,gridtitle,gridcolorr);
break;
default:
return;
}
if(lastPos<position) {
Animation animation = AnimationUtils.loadAnimation(holder.itemView.getContext(), R.anim.fade_in);
holder.itemView.setAnimation(animation);
lastPos=position;
}
}
#Override
public int getItemCount() {
return myMallModelList.size();
}
public class BannerSliderViewHolder extends RecyclerView.ViewHolder {
private ViewPager bannerSliderViewPager;
private int currentPage;
private Timer timer;
final private long DELAY_TIME=3000;
final private long PERIOD_TIME=3000;
private List<SliderModel> arrangedList;
public BannerSliderViewHolder(#NonNull View itemView) {
super(itemView);
bannerSliderViewPager=itemView.findViewById(R.id.banner_slider_view_pager);
}
public void setBannerSliderViewPager(final List<SliderModel> sliderModelList){
currentPage=2;
if(timer!=null){
timer.cancel();
}
arrangedList=new ArrayList<>();
for(int x=0;x<sliderModelList.size();x++){
arrangedList.add(x,sliderModelList.get(x));
}
arrangedList.add(0,sliderModelList.get(sliderModelList.size()-2));
arrangedList.add(1,sliderModelList.get(sliderModelList.size()-1));
arrangedList.add(sliderModelList.get(0));
arrangedList.add(sliderModelList.get(1));
SliderAdapter sliderAdapter=new SliderAdapter(arrangedList);
bannerSliderViewPager.setAdapter(sliderAdapter);
bannerSliderViewPager.setClipToPadding(false);
bannerSliderViewPager.setPageMargin(20);
bannerSliderViewPager.setCurrentItem(currentPage);
ViewPager.OnPageChangeListener onPageChangeListener=new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
currentPage=position;
}
#Override
public void onPageScrollStateChanged(int state) {
if(state==ViewPager.SCROLL_STATE_IDLE){
pageLooper(arrangedList);
}
}
};
bannerSliderViewPager.addOnPageChangeListener(onPageChangeListener);
startBannerSlideShow(arrangedList);
bannerSliderViewPager.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
pageLooper(arrangedList);
stopBannerSlideShow();
if(motionEvent.getAction()==MotionEvent.ACTION_UP){
startBannerSlideShow(arrangedList);
}
return false;
}
});
}
private void pageLooper(List<SliderModel> sliderModelList){
if(currentPage==sliderModelList.size()-2){
currentPage=2;
bannerSliderViewPager.setCurrentItem(currentPage,false);
}
if(currentPage==1){
currentPage=sliderModelList.size()-3;
bannerSliderViewPager.setCurrentItem(currentPage,false);
}
}
private void startBannerSlideShow(final List<SliderModel> sliderModelList){
final Handler handler=new Handler();
final Runnable update=new Runnable() {
#Override
public void run() {
if(currentPage>=sliderModelList.size()){
currentPage=1;
}
bannerSliderViewPager.setCurrentItem(currentPage++,true);
}
};
timer=new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
handler.post(update);
}
},DELAY_TIME,PERIOD_TIME);
}
private void stopBannerSlideShow(){
timer.cancel();
}
}
public class StripAdBannerViewHolder extends RecyclerView.ViewHolder{
private ImageView stripAdImage;
private ConstraintLayout stripAdContainer;
public StripAdBannerViewHolder(#NonNull View itemView) {
super(itemView);
stripAdImage=itemView.findViewById(R.id.strip_ad_image);
stripAdContainer=itemView.findViewById(R.id.strip_ad_container);
}
public void StripAd(String resource,String color){
Glide.with(itemView.getContext()).load(resource).apply(new RequestOptions().placeholder(R.mipmap.pic)).into(stripAdImage);
stripAdContainer.setBackgroundColor(Color.parseColor(color));
}
}
public class HorizontalProductViewHolder extends RecyclerView.ViewHolder{
private ConstraintLayout container;
private TextView horizontallayoutTitle;
private Button horizontalviewAllBtn;
private RecyclerView horizontalRecyclerView;
public HorizontalProductViewHolder(#NonNull View itemView) {
super(itemView);
container=itemView.findViewById(R.id.container);
horizontalRecyclerView=itemView.findViewById(R.id.horizontal_scroll_layout_recycler_view);
horizontalviewAllBtn=itemView.findViewById(R.id.horizontal_scroll_layout_viewall_button);
horizontallayoutTitle=itemView.findViewById(R.id.horizontal_scroll_layout_title);
horizontalRecyclerView.setRecycledViewPool(recycledViewPool);
}
private void setHorizontalProductLayout(final List<HorizontalProductScrollModel> horizontalProductScrollModelList, final String title, String color, final List<WishlistModel> viewAllProductList){
container.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor(color)));
horizontallayoutTitle.setText(title);
for (final HorizontalProductScrollModel model:horizontalProductScrollModelList) {
if (!model.getProductID().isEmpty() && model.getProductTitle().isEmpty()){
firebaseFirestore.collection("PRODUCTS").document(model.getProductID()).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
#Override
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()){
model.setProductTitle(task.getResult().getString("product_title"));
model.setProductPrice(task.getResult().getString("product_price"));
model.setProductImage(task.getResult().getString("product_image_1"));
WishlistModel wishlistModel = viewAllProductList.get(horizontalProductScrollModelList.indexOf(model));
wishlistModel.setTotalRatings(task.getResult().getLong("total_ratings"));
wishlistModel.setRating(task.getResult().getString("average_rating"));
wishlistModel.setProductTitle(task.getResult().getString("product_title"));
wishlistModel.setProductPrice(task.getResult().getString("product_price"));
wishlistModel.setProductImage(task.getResult().getString("product_image_1"));
wishlistModel.setFreeCoupans(task.getResult().getLong("free_coupans"));
wishlistModel.setCuttedPrice(task.getResult().getString("cutted_price"));
wishlistModel.setCOD(task.getResult().getBoolean("COD"));
wishlistModel.setInStock(task.getResult().getLong("stock_quantity") > 0);
if (horizontalProductScrollModelList.indexOf(model) == horizontalProductScrollModelList.size()-1){
if (horizontalRecyclerView.getAdapter()!= null){
horizontalRecyclerView.getAdapter().notifyDataSetChanged();
}
}
}else{
//do nothing
}
}
});
if(horizontalProductScrollModelList.size() > 8){
horizontalviewAllBtn.setVisibility(View.VISIBLE);
horizontalviewAllBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ViewAllActivity.wishlistModelList=viewAllProductList;
itemView.getContext().startActivity(new Intent(itemView.getContext(),ViewAllActivity.class)
.putExtra("layout",0)
.putExtra("title",title)
);
}
});
}else {
horizontalviewAllBtn.setVisibility(View.INVISIBLE);
}
}
}
HorizontalProductScrollAdapter horizontalProductScrollAdapter=new HorizontalProductScrollAdapter(horizontalProductScrollModelList);
LinearLayoutManager linearLayoutManager1 = new LinearLayoutManager(itemView.getContext());
linearLayoutManager1.setOrientation(LinearLayoutManager.HORIZONTAL);
horizontalRecyclerView.setLayoutManager(linearLayoutManager1);
horizontalRecyclerView.setAdapter(horizontalProductScrollAdapter);
horizontalProductScrollAdapter.notifyDataSetChanged();
}
}
public class GridProductViewHolder extends RecyclerView.ViewHolder{
private ConstraintLayout container;
private TextView gridLayoutTitle;
private Button gridLayoutButton;
private GridLayout gridProductLayout;
public GridProductViewHolder(#NonNull final View itemView) {
super(itemView);
container=itemView.findViewById(R.id.container);
gridLayoutTitle=itemView.findViewById(R.id.grid_product_layout_title);
gridLayoutButton=itemView.findViewById(R.id.grid_product_layout_viewall_button);
gridProductLayout=itemView.findViewById(R.id.grid_layout);
}
private void setGridProductLayout(final List<HorizontalProductScrollModel> horizontalProductScrollModelList, final String title, String color) {
container.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor(color)));
gridLayoutTitle.setText(title);
for(int x=0;x<4;x++){
ImageView productImage=gridProductLayout.getChildAt(x).findViewById(R.id.h_s_product_image);
TextView productTitle=gridProductLayout.getChildAt(x).findViewById(R.id.h_s_product_title);
TextView productDesc=gridProductLayout.getChildAt(x).findViewById(R.id.h_s_product_description);
TextView productPrice=gridProductLayout.getChildAt(x).findViewById(R.id.h_s_product_price);
Glide.with(itemView.getContext()).load(horizontalProductScrollModelList.get(x).getProductImage()).apply(new RequestOptions().placeholder(R.mipmap.pic)).into(productImage);
productTitle.setText(horizontalProductScrollModelList.get(x).getProductTitle());
productDesc.setText(horizontalProductScrollModelList.get(x).getProductDesc());
productPrice.setText("Rs."+horizontalProductScrollModelList.get(x).getProductPrice()+"/-");
gridProductLayout.getChildAt(x).setBackgroundColor(Color.WHITE);
if(!title.equals("")){
final int finalX = x;
gridProductLayout.getChildAt(x).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
itemView.getContext().startActivity(new Intent(itemView.getContext(),ProductDetailsActivity.class).putExtra("PRODUCT_ID",horizontalProductScrollModelList.get(finalX).getProductID()));
}
});
}
}
if(!title.equals("")) {
gridLayoutButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ViewAllActivity.horizontalProductScrollModelList = horizontalProductScrollModelList;
itemView.getContext().startActivity(new Intent(itemView.getContext(), ViewAllActivity.class)
.putExtra("layout", 1)
.putExtra("title", title)
);
}
});
}
}
}
}
please try to help me i am searching for a solution from so long i tried everything but noting worked if you could tell me where i am wrong it will be very greatful.
I have a recycle view which populates data from a server, the components inside are a textView and a Switch. The server can return n number of data. How can i set a unique id to the Switch2 when I am populating the data, because later I will need to set a listener to the Switches, My server actually returns a unique id but I'm not so sure on how to set it to the Switch2, or is there any alternate parameters that can be used to identify the Switch?
layout
<?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="wrap_content"
tools:layout_editor_absoluteY="81dp">
<android:android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="1dp"
card_view:cardUseCompatPadding="true"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Switch
android:id="#+id/switch2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left|center_vertical"
android:paddingLeft="5dip"
android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="254dp" />
<TextView
android:id="#+id/user_set_light_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:fontFamily="sans-serif"
android:text="TextView"
android:textSize="20dp"
tools:layout_editor_absoluteX="27dp"
tools:layout_editor_absoluteY="23dp" />
</RelativeLayout>
</android:android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>
adapter
public class populateLights_adapter extends RecyclerView.Adapter<populateLights_adapter.ViewHolder> {
private List<populate_lights> listItems;
private Context context;
public populateLights_adapter(List<populate_lights> listItems, Context context) {
this.listItems = listItems;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.addlight_items, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
populate_lights listItem = listItems.get(position);
holder.lightText.setText(listItem.getLightName());
holder.status.setChecked(listItem.getState());
}
#Override
public int getItemCount() {
return listItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView lightText;
public Switch status;
public ViewHolder(View itemView) {
super(itemView);
lightText = (TextView) itemView.findViewById(R.id.user_set_light_id);
status = (Switch) itemView.findViewById(R.id.switch2);
}
}
}
java class
public class populate_lights {
private String lightName;
private boolean state;
public populate_lights(String lightName, boolean state){
this.lightName = lightName;
this.state = state;
}
public String getLightName(){
return lightName;
}
public boolean getState(){
return state;
}
}
main
public class lightsControl extends Fragment {
View myView;
public static final String URL = "serverurl.com";
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private List<populate_lights> listItems;
private Switch mySwitch;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.lightscontrol, container, false);
recyclerView = (RecyclerView) myView.findViewById(R.id.lightsView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
listItems = new ArrayList<>();
loadData();
adapter = new populateLights_adapter(listItems, myView.getContext());
recyclerView.setAdapter(adapter);
return myView;
}
private void loadData(){
final ProgressDialog progressDialog = new ProgressDialog(myView.getContext());
progressDialog.setMessage("Loading");
progressDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.POST,
URL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
progressDialog.dismiss();
Snackbar mySnackbar = Snackbar.make(myView, "Data Fetched!", Snackbar.LENGTH_SHORT);
mySnackbar.show();
Log.v("DATA_RESPONSE", response);
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray array = jsonObject.getJSONArray("lightData");
for(int i=0;i<array.length();i++){
JSONObject obj = array.getJSONObject(i);
Log.v("LIGHT ID ", "index=" + obj.getString("LightID"));
Log.v("Value ", "index=" + obj.getBoolean("Value"));
populate_lights popLights = new populate_lights(
obj.getString("LightID"), //unique id
obj.getBoolean("Value") //value returns true, or false
);
listItems.add(popLights);
}
adapter = new populateLights_adapter(listItems, myView.getContext());
recyclerView.setAdapter(adapter);
}
catch(Exception e){
e.printStackTrace();
}
}},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Snackbar mySnackbar = Snackbar.make(myView, "Oops, there was an error communicating with our server, try again", Snackbar.LENGTH_SHORT);
mySnackbar.show();
Log.v("LoginFormERROR", "index=" + error);
progressDialog.dismiss();
}
}
)
};
RequestQueue requestQueue = Volley.newRequestQueue(myView.getContext());
requestQueue.add(stringRequest);
}
public void showErrorAlert(){
AlertDialog.Builder builder1 = new AlertDialog.Builder(myView.getContext());
builder1.setMessage("Opps, something went wring");
builder1.setCancelable(true);
builder1.setPositiveButton(
"Main Menu",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
getActivity().onBackPressed();
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
}
}
screenshot
See if this code prints the right position in the logs. Put this inside the constructor of the view holder in the adapter class:
status.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
Log.d("Position: ", String.valueOf(getLayoutPosition()));
}
});
I want to display UUID and Minor code of found beacons using listview in android.
I think I have problem with custom adapter.
Here is my actual code:
FoundBeacons.java
public class FoundBeacon extends Activity implements BeaconConsumer {
private BeaconManager beaconManager;
private ListView listview;
private ArrayList<Beacon> beaconList;
private AdapterBeacon bAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_found_beacon);
beaconManager = BeaconManager.getInstanceForApplication(this);
beaconManager.bind(this);
beaconList = new ArrayList<Beacon>();
this.bAdapter = new AdapterBeacon(this, beaconList);
listview = (ListView) findViewById(R.id.listview);
listview.setAdapter(bAdapter);
}
#Override
public void onBeaconServiceConnect() {
beaconManager.addRangeNotifier(new RangeNotifier() {
#Override
public void didRangeBeaconsInRegion(final Collection<Beacon> beacons, Region region) {
if (beacons.size() > 0) {
runOnUiThread(new Runnable() {
#Override
public void run() {
bAdapter.copyBeacons(beacons);
listview.setAdapter(bAdapter);
}
});
}
}
});
try {
beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
} catch (RemoteException e) {
}
}
}
AdapterBeacon.java
public class AdapterBeacon extends ArrayAdapter<Beacon> {
private Context context;
private ArrayList<Beacon> allBeacons;
private LayoutInflater mInflater;
private boolean mNotifyOnChange = true;
public AdapterBeacon(Context context, ArrayList<Beacon> mBeacons) {
super(context, R.layout.row_beacon);
this.context = context;
this.allBeacons = new ArrayList<Beacon>(mBeacons);
this.mInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return allBeacons .size();
}
#Override
public Beacon getItem(int position) {
return allBeacons .get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public int getPosition(Beacon item) {
return allBeacons .indexOf(item);
}
#Override
public int getViewTypeCount() {
return 1; //Number of types + 1 !!!!!!!!
}
#Override
public int getItemViewType(int position) {
return 1;
}
public void copyBeacons(Collection<Beacon> beacons)
{
allBeacons.clear();
for (Beacon b : beacons)
{
allBeacons.add(b);
}
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
int type = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case 1:
convertView = mInflater.inflate(R.layout.row_beacon,parent, false);
holder.name = (TextView) convertView.findViewById(R.id.t1);
holder.description = (TextView) convertView.findViewById(R.id.t2);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.name.setText(allBeacons.get(position).getId1().toString());
holder.description.setText(allBeacons.get(position).getId2().toString());
holder.pos = position;
return convertView;
}
#Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
mNotifyOnChange = true;
}
public void setNotifyOnChange(boolean notifyOnChange) {
mNotifyOnChange = notifyOnChange;
}
//---------------static views for each row-----------//
static class ViewHolder {
TextView name;
TextView description;
int pos; //to store the position of the item within the list
}
}
activity_found_beacon.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_found_beacon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.pio.pd_inz_ibeacon.FoundBeacon">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="#+id/listview" />
row_beacon.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/t1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text"/>
<TextView
android:id="#+id/t2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text"/>
<TextView
android:id="#+id/t3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text"/>
I have implemented an abstract class that has a List View like so:
public abstract class ExampleListFragment extends ExampleFragment{
protected ListView _listView;
public ExampleListFragment () {
super();
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//Set Up the Bottom Bar
View view = super.onCreateView(inflater, container, savedInstanceState);
addList();
return view;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
public void addList() {
_listView = new ListView(getActivity());
RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
parms.addRule(RelativeLayout.CENTER_HORIZONTAL);
parms.addRule(RelativeLayout.BELOW, R.id.tableLayout);
_listView.setLayoutParams(parms);
_listView.setClickable(true);
_listView.setFocusable(true);
_listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
longListClick(i);
return true;
}
});
_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
listClick(i);
}
});
_layout.addView(_listView);
}
public abstract void longListClick(int pos);
public abstract void listClick(int pos);
}
I then have a class that inherits from this to implement the abstract methods which should be called when clicking an item in the list
public class TwitterFragment extends ExampleListFragment {
private TwitterAdapter _adapter;
//...
#Override
public void onSelected() {
_adapter = new TwitterAdapter(getActivity());
populateAdapter();
}
private void populateAdapter() {
showLoadingTweets();
new TwitterGetTimelineTask().execute(username); //fills adapter with Twitter status info...
}
#Override
public void longListClick(int pos) {
Toast toast = Toast.makeText(getActivity(),String.format("You have clicked item number: %d",pos),
Toast.LENGTH_SHORT);
toast.show();
}
#Override
public void listClick(int pos) {
Toast toast = Toast.makeText(getActivity(),String.format("You have clicked item number: %d",pos),
Toast.LENGTH_SHORT);
toast.show();
}
//...
class TwitterGetTimelineTask extends AsyncTask<String, String, String> {
//...
}
}
However, whenever I click on an Item, it does not appear to even enter onItemClick functions.
Any ideas? help much appreciated.
*EDIT*
As requested, here is the code for my Adapter
public class TwitterAdapter extends BaseAdapter {
private ArrayList<StatusDetails> _statusDetailsArrayList;
private LayoutInflater _inflater;
public TwitterAdapter(Context context) {
_statusDetailsArrayList = new ArrayList<StatusDetails>();
_inflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return _statusDetailsArrayList.size();
}
#Override
public Object getItem(int i) {
return _statusDetailsArrayList.get(i);
}
#Override
public long getItemId(int i) {
return _statusDetailsArrayList.get(i).get_id();
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder;
if (view == null) {
view = _inflater.inflate(R.layout.twitter_layout, null);
holder = new ViewHolder();
holder.img_icon = (ImageView) view.findViewById(R.id.statusImage);
holder.text_line1 = (TextView) view.findViewById(R.id.statusText);
holder.text_line2 = (TextView) view.findViewById(R.id.statusDateText);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
Bitmap bmp = _statusDetailsArrayList.get(i).get_profile();
if (bmp == null) {
holder.img_icon.setImageResource(R.drawable.ic_action_twitter);
} else {
holder.img_icon.setImageBitmap(bmp);
}
holder.text_line1.setText(_statusDetailsArrayList.get(i).get_text());
holder.text_line2.setText(_statusDetailsArrayList.get(i).get_date());
return view;
}
public void addStatus(StatusDetails details) {
_statusDetailsArrayList.add(details);
}
public void setStatuses(ArrayList<StatusDetails> details) {
_statusDetailsArrayList = details;
}
public ArrayList<StatusDetails> getEvents() {
return _statusDetailsArrayList;
}
public void removeAll() {
_statusDetailsArrayList.clear();
}
private class ViewHolder {
ImageView img_icon;
TextView text_line1;
TextView text_line2;
}
}
And if needed, the XML of the layout of each list
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/statusImage"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Status"
android:id="#+id/statusText"
android:layout_alignParentTop="true"
android:autoLink="web"
android:textColor="#android:color/white"
android:layout_toRightOf="#+id/statusImage" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_below="#id/statusText"
android:text="Date"
android:id="#+id/statusDateText"
android:layout_alignBottom="#+id/statusImage"
android:layout_toRightOf="#+id/statusImage" />
Thanks.
Adapter
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context mContext;
private ExpandableListView mExpandableListView;
private List <Grupe> mGroupCollection;
private int[] groupStatus;
public ExpandableListAdapter() {}
public ExpandableListAdapter(Context pContext,
ExpandableListView pExpandableListView,
List <Grupe> pGroupCollection) {
mContext = pContext;
mGroupCollection = pGroupCollection;
mExpandableListView = pExpandableListView;
groupStatus = new int[mGroupCollection.size()];
setListEvent();
}
private void setListEvent() {
mExpandableListView
.setOnGroupExpandListener(new OnGroupExpandListener() {
#Override
public void onGroupExpand(int arg0) {
groupStatus[arg0] = 1;
}
});
mExpandableListView
.setOnGroupCollapseListener(new OnGroupCollapseListener() {
#Override
public void onGroupCollapse(int arg0) {
groupStatus[arg0] = 0;
}
});
}
#Override
public String getChild(int arg0, int arg1) {
return mGroupCollection.get(arg0).getChildren().get(arg0).getPrimaoc_Poruke();
}
#Override
public long getChildId(int arg0, int arg1) {
return 0;
}
#Override
public View getChildView(int arg0, int arg1, boolean arg2, View arg3,
ViewGroup arg4) {
ChildHolder childHolder;
if (arg3 == null) {
arg3 = LayoutInflater.from(mContext).inflate(
R.layout.list_group_item, null);
childHolder = new ChildHolder();
childHolder.title = (TextView) arg3.findViewById(R.id.item_title);
arg3.setTag(childHolder);
} else {
childHolder = (ChildHolder) arg3.getTag();
}
childHolder.title.setText(mGroupCollection.get(arg0).getChildren().get(arg1).getPrimaoc_Poruke());
return arg3;
}
#Override
public int getChildrenCount(int arg0) {
return mGroupCollection.get(arg0).getChildren().size();
}
#Override
public Object getGroup(int arg0) {
return mGroupCollection.get(arg0);
}
#Override
public int getGroupCount() {
return mGroupCollection.size();
}
#Override
public long getGroupId(int arg0) {
return arg0;
}
#Override
public View getGroupView(int arg0, boolean arg1, View arg2, ViewGroup arg3) {
GroupHolder groupHolder;
if (arg2 == null) {
arg2 = LayoutInflater.from(mContext).inflate(R.layout.list_group, null);
groupHolder = new GroupHolder();
groupHolder.img = (ImageView) arg2.findViewById(R.id.tag_img);
groupHolder.title = (TextView) arg2.findViewById(R.id.group_title);
arg2.setTag(groupHolder);
} else {
groupHolder = (GroupHolder) arg2.getTag();
}
if (groupStatus[arg0] == 0) {
groupHolder.img.setImageResource(R.drawable.group_down);
} else {
groupHolder.img.setImageResource(R.drawable.group_up);
}
groupHolder.title.setText(mGroupCollection.get(arg0).getTip());
return arg2;
}
class GroupHolder {
ImageView img;
TextView title;
}
class ChildHolder {
TextView title;
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public boolean isChildSelectable(int arg0, int arg1) {
return true;
}
}
MainActivity
public class Glavna extends Activity implements OnClickListener, OnChildClickListener {
private List <Grupe> mGroupCollection;
private ExpandableListView mExpandableListView;
private ExpandableListAdapter adapter = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_glavna);
prepareResource();
initPage();
}
private void initPage() {
mExpandableListView = (ExpandableListView) findViewById(R.id.expandableListView1);
adapter = new ExpandableListAdapter(this,mExpandableListView, mGroupCollection);
mExpandableListView.setAdapter(adapter);
mExpandableListView.setOnChildClickListener(this);
}
private void prepareResource() {
mGroupCollection = new ArrayList <Grupe> ();
ge.setTip("Online korisnici");
mGroupCollection.add(ge);
AktivniChat gi = new AktivniChat(1, "receiver 1", new Korisnik(1, "sender1"));
mGroupCollection.get(0).setAktivniChat(gi);
}
#Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
return true;
}
}
Set android:focusable="false" on all elements.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:baselineAligned="false"
android:paddingTop="5dip"
android:focusable="false">
<LinearLayout
android:id="#+id/groupItem"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#FFF385"
android:clickable="false"
android:focusable="false">
<TextView
android:id="#+id/item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FF84"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="sample"
android:textColor="#000000"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
Don't forget to enable child Selectable property..
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
It worked for me too... All items in layout (of the child) must be set to focusable="false" and clickable="false". But if you have buttons in child layout this will not work.