填充listview的有效方法

时间:2016-03-29 19:59:44

标签: android listview android-recyclerview

每次我点击不同的卡片时,都会像EvsActivity一样调用一个活动。

但如果没有。卡是100然后我必须创建100活动,这不是一个可行的解决方案。

所以任何有更好解决方案的人都会受到赞赏

//带有cardview代码的Recyclerview

package com.studyleague.app;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class AvailableContent extends TemplateFragment {
    @Bind(R.id.available_recycler_view)
    RecyclerView recyclerView;

    public AvailableContent() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.available_content, container, false);
        ButterKnife.bind(this, v);
        recyclerView.setHasFixedSize(true);
        GridLayoutManager glm = new GridLayoutManager(getActivity(), 2, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(glm);

        List<AvailableContentModel> content = new ArrayList<>();
        for (int i = 0; i < AvailableContentData.acName.length; i++) {
            content.add(new AvailableContentModel(AvailableContentData.acIcon[i], AvailableContentData.acName[i]));
        }

        final AvailableContentAdapter contentAdapter = new AvailableContentAdapter(getActivity(), content);
        recyclerView.setAdapter(contentAdapter);



        return v;

    }


}

// //适配器

package com.studyleague.app;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
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.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class AvailableContentAdapter extends RecyclerView.Adapter<AvailableContentAdapter.ContentViewHolder> {

    private final List<AvailableContentModel> dataSet;
    private Context context;
    public ArrayList<AvailableContentModel> content;

    public AvailableContentAdapter(Context context, List<AvailableContentModel> content) {
        this.context = context;
        this.dataSet = content;
    }

    @Override
    public ContentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.available_content_card, parent, false);
        return new ContentViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ContentViewHolder hold, final int listPosition) {
        hold.acImg.setImageResource(dataSet.get(listPosition).getContentImg());
        hold.acText.setText(dataSet.get(listPosition).getContentName());
    }

    @Override
    public int getItemCount() {
        return dataSet.size();
    }

    public class ContentViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.available_card_image_view)
        ImageView acImg;
        @Bind(R.id.available_card_text_view)
        TextView acText;

        public ContentViewHolder(final View view) {
            super(view);
            ButterKnife.bind(this, view);

            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    switch (getAdapterPosition()) {
                        case 0:
                            Toast.makeText(v.getContext(), "Mechanics", Toast.LENGTH_SHORT).show();
                            break;
                        case 1:
                            Toast.makeText(v.getContext(), "B. E. E.", Toast.LENGTH_SHORT).show();
                            break;
                        case 2:
                            Toast.makeText(v.getContext(), "Maths", Toast.LENGTH_SHORT).show();
                            break;
                        case 3:
                            Toast.makeText(v.getContext(), "Chemistry", Toast.LENGTH_SHORT).show();
                            break;
                        case 4:
                            Toast.makeText(v.getContext(), "Physics", Toast.LENGTH_SHORT).show();
                            break;
                        case 5:
                            context.startActivity(new Intent(context, EvsActivity.class));
                            break;
                        default:
                            Toast.makeText(v.getContext(), "YOLO", Toast.LENGTH_SHORT).show();
                            break;
                    }
                }
            });


        }
    }


}

//点击其中一张卡片时调用的活动//

package com.studyleague.app;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class EvsActivity extends AppCompatActivity {
    @Bind(R.id.exp_list)
    ExpandableListView expListView;

    private List<String> chap;
    private HashMap<String, List<String>> hashMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_evs);
        ButterKnife.bind(this);

        // preparing list data
        prepareListData();

        // setting list adapter
        ExpandableListAdapter listAdapter = new ExpandableListAdapter(this, chap, hashMap);
        expListView.setAdapter(listAdapter);

        // expListView on child click listener
        expListView.setOnChildClickListener(new OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                                        int groupPosition, int childPosition, long id) {
                String groupName = chap.get(groupPosition);
                String childName = hashMap.get(groupName).get(childPosition);
                Toast.makeText(getApplication(), groupName + " : " + childName, Toast.LENGTH_SHORT)
                        .show();
                Intent i = new Intent(EvsActivity.this, Study.class);
                startActivity(i);
                return false;
            }
        });
    }

    /*
     * Preparing the list data
     */
    private void prepareListData() {

        // Hash map for both header and child
        hashMap = new HashMap<>();

        // Array list for header
        chap = new ArrayList<>();

        // Adding headers to list
        chap.add("1. Multidisciplinary Nature of Environmental Studies");
        chap.add("2. Sustainable Development");
        chap.add("3. Environmental Pollution");
        chap.add("4. Environmental Legislation");
        chap.add("5. Renewable Sources of Energy");
        chap.add("6. Environment and Technology");
        chap.add("7. Another Chapter");
        chap.add("8. Almost Another Chapter");

        // Array list for child items
        List<String> child1 = new ArrayList<>(fillChildData(1, 5));
        List<String> child2 = new ArrayList<>(fillChildData(2, 3));
        List<String> child3 = new ArrayList<>(fillChildData(3, 11));
        List<String> child4 = new ArrayList<>(fillChildData(4, 6));
        List<String> child5 = new ArrayList<>(fillChildData(5, 4));
        List<String> child6 = new ArrayList<>(fillChildData(6, 2));
        List<String> child7 = new ArrayList<>(fillChildData(7, 2));
        List<String> child8 = new ArrayList<>(fillChildData(8, 2));

        // Adding header and children to hash map
        hashMap.put(chap.get(0), child1);
        hashMap.put(chap.get(1), child2);
        hashMap.put(chap.get(2), child3);
        hashMap.put(chap.get(3), child4);
        hashMap.put(chap.get(4), child5);
        hashMap.put(chap.get(5), child6);
        hashMap.put(chap.get(6), child7);
        hashMap.put(chap.get(7), child8);
    }

    private Collection<String> fillChildData(int index, int n) {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add("Group " + index + " - Child : " + (i + 1));
        }
        return list;
    }
}

//需要的数据代码//

package com.studyleague.app;

public class AvailableContentData {

    static final Integer[] acIcon = {
            R.mipmap.mec,
            R.mipmap.elec,
            R.mipmap.mat,
            R.mipmap.chem,
            R.mipmap.ic,
            R.mipmap.env
    };
    static final String[] acName = {
            "Mechanics",
            "B. E. E.",
            "Maths",
            "Chemistry",
            "Physics",
            "E. V. S."
    };
}

1 个答案:

答案 0 :(得分:0)

在错误的帐户下输入(拒绝评论,对不起),我猜不是。使用一个ListView创建一个Activity,并从CardView intent传入要使用的适配器。

Butterfly Cardview - &gt; BUTTERFLY - &gt; ButterflyAdapter

棒球棒Cardview - &gt;棒球 - &gt; BaseballBatAdatper

如果您的商品不相关。如果它们相关,您可以使用相同的适配器,但根据您发送的内容发送不同的布局。

蝴蝶 - &gt; R.layout.butterfly_items

鲸鱼 - &gt; R.layout.whale_items