ExpandableListView子单击侦听器无法正常工作

时间:2018-06-16 09:02:55

标签: java android onclicklistener buttonclick

不确定为什么我的onChildClick没有触发。一切都很完美,除了当其中一个儿童用品被敲击时,绝对没有任何反应。否则,可扩展组按预期工作。 问题是程序永远不会进入onChildClick()事件处理程序。有什么想法吗?

MainActivity.java

public class MainActivity extends AppCompatActivity {

private ExpandableListView mBrandsListView;
HashMap<String, ArrayList<DataInfoTivi>> expandableListDetail;
private ExpandableListAdapter mBrandAdapter;
List<String> expandableListTitle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mBrandsListView = (ExpandableListView) findViewById(R.id.list_brands);
    expandableListDetail = ExpandableListDataPump.getData();
    expandableListTitle = new ArrayList<String>(expandableListDetail.keySet());
    mBrandAdapter = new BrandAdapter(this,expandableListTitle ,expandableListDetail);
    mBrandsListView.setAdapter(mBrandAdapter);

    mBrandsListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(final ExpandableListView parent, final View v, final int groupPosition, final int childPosition, final long id) {
            Log.e("Click ne","buc ghe");
            return false;
        }
    });

    for (int i = 0; i < mBrandAdapter.getGroupCount(); i++) {
        mBrandsListView.expandGroup(i);
    }
}

BrandAdapter.java

public class BrandAdapter implements ExpandableListAdapter {

    private Context context;
    private HashMap<String, ArrayList<DataInfoTivi>> expandableListDetail1;
    List<String> expandableListTitle1;
    public BrandAdapter(Context context, List<String> expandableListTitle, HashMap<String, ArrayList<DataInfoTivi>> expandableListDetail) {
        this.context = context;
        this.expandableListDetail1 = expandableListDetail;
        this.expandableListTitle1 = expandableListTitle;
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {

    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {

    }

    @Override
    public int getGroupCount() {
        return expandableListDetail1.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return expandableListDetail1.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return expandableListDetail1.get(groupPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        ParentHolder parentHolder = null;

        if(convertView == null) {
            LayoutInflater userInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = userInflater.inflate(R.layout.item_parent, null);
//            convertView.setHorizontalScrollBarEnabled(true);

            parentHolder = new ParentHolder();
            convertView.setTag(parentHolder);

        } else {
            parentHolder = (ParentHolder) convertView.getTag();
        }

        parentHolder.brandName = (TextView) convertView.findViewById(R.id.text_brand);
        parentHolder.brandName.setText(expandableListTitle1.get(groupPosition).toString());

        parentHolder.indicator = (ImageView) convertView.findViewById(R.id.image_indicator);

//        if(isExpanded) {
//            parentHolder.indicator.setImageResource(R.drawable.ic_keyboard_arrow_up_black_18dp);
//        } else {
//            parentHolder.indicator.setImageResource(R.drawable.ic_keyboard_arrow_down_black_18dp);
//        }

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        ChildHolder childHolder = null;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.item_group_child, parent, false);
            childHolder = new ChildHolder();
            convertView.setTag(childHolder);
        }
        else {
            childHolder = (ChildHolder) convertView.getTag();
        }

        childHolder.horizontalListView = (RecyclerView) convertView.findViewById(R.id.mobiles);
        GridLayoutManager lLayout = new GridLayoutManager(context, 3);
//        LinearLayoutManager layoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
        childHolder.horizontalListView.setLayoutManager(lLayout);
        ArrayList<DataInfoTivi> mDataInfoTivisArayList = expandableListDetail1.get(this.expandableListTitle1.get(groupPosition));
        MobileAdapter horizontalListAdapter = new MobileAdapter(context, mDataInfoTivisArayList);
        childHolder.horizontalListView.setAdapter(horizontalListAdapter);
        convertView.setClickable(false);

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public void onGroupExpanded(int groupPosition) {

    }

    @Override
    public void onGroupCollapsed(int groupPosition) {

    }

    @Override
    public long getCombinedChildId(long groupId, long childId) {
        return 0;
    }

    @Override
    public long getCombinedGroupId(long groupId) {
        return 0;
    }

    private static class ChildHolder {
        static RecyclerView horizontalListView;
    }

    private static class ParentHolder {
        TextView brandName;
        ImageView indicator;
    }

MobileAdapter.java

public class MobileAdapter extends RecyclerView.Adapter<MobileAdapter.ViewHolder> {

    private Context context;
    private ArrayList<DataInfoTivi> mobiles1=null;
    ImageLoader imageLoader = ImageLoader.getInstance();
    DisplayImageOptions defaultOptions = null;
    public MobileAdapter(Context context, ArrayList<DataInfoTivi> mobiles) {
        this.context = context;
        this.mobiles1 = mobiles;
        defaultOptions = new DisplayImageOptions.Builder().cacheInMemory()
                .cacheOnDisc().build();
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                context).defaultDisplayImageOptions(defaultOptions).build();
        imageLoader.init(config);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View cardView = inflater.inflate(R.layout.item_child, null, false);
        ViewHolder viewHolder = new ViewHolder(cardView);
        viewHolder.mobileImage = (ImageView) cardView.findViewById(R.id.image_mobile);
        viewHolder.mLinearLayout = (LinearLayout) cardView.findViewById(R.id.layout);
        viewHolder.modelName = (TextView) cardView.findViewById(R.id.text_mobile_model);
        return viewHolder;
    }

    int a;
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        a = position;
        ImageView mobileImageView = (ImageView) holder.mobileImage;
        imageLoader.displayImage(mobiles1.get(position).getUrlimage(), mobileImageView);
        TextView modelTextView = (TextView) holder.modelName;
        modelTextView.setText(mobiles1.get(position).getTiviName());
//
//        TextView priceTextView = (TextView) holder.price;
//        priceTextView.setText(mobiles.get(position).price);

    }

    @Override
    public int getItemCount() {

        return mobiles1.size();
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout mLinearLayout;
        ImageView mobileImage;
        TextView modelName;

        public ViewHolder(View itemView) {
            super(itemView);
            mobileImage = (ImageView) itemView.findViewById(R.id.image_mobile);
//            modelName = (TextView) itemView.findViewById(R.id.text_mobile_model);
//            price = (TextView) itemView.findViewById(R.id.text_mobile_price);
        }
    }

activitymain.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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#d6d6d6"
    tools:context="com.aurum.activities.MainActivity">

    <ExpandableListView
        android:id="@+id/list_brands"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@null"
        android:groupIndicator="@null"
        android:dividerHeight="5dp" />


</RelativeLayout>

item_child.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_margin="3dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <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"
            android:padding="10dp">

            <LinearLayout
                android:id="@+id/layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:gravity="center_horizontal"
                android:orientation="vertical">

                <ImageView
                    android:id="@+id/image_mobile"
                    android:layout_width="70dp"
                    android:layout_height="80dp"
                    android:adjustViewBounds="true" />
                <TextView
                    android:textColor="#000"
                    android:textSize="12dp"
                    android:gravity="center_vertical|center_horizontal"
                    android:id="@+id/text_mobile_model"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:lines="1" />


            </LinearLayout>
        </android.support.v7.widget.CardView>
    </LinearLayout>
</RelativeLayout>

item_parent.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="wrap_content"
    android:background="#4e4e4e"
    android:gravity="center_vertical"
    android:orientation="horizontal">

    <RelativeLayout
        android:padding="5dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:textStyle="bold"
            android:id="@+id/text_brand"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Text"
            android:textColor="#fff"
            android:textSize="15dp" />

        <ImageView
            android:id="@+id/image_indicator"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentTop="true"
            android:layout_marginEnd="11dp"
            android:src="@drawable/ic_keyboard_arrow_down_black_18dp" />
    </RelativeLayout>
</LinearLayout>

0 个答案:

没有答案