【问题标题】:How to make a listview fragment with image?如何用图像制作列表视图片段?
【发布时间】:2018-02-19 16:31:02
【问题描述】:

我尝试将此代码放在其他项目中,但它不起作用。

ListView 活动项目

MainActivity

public class MainActivity extends AppCompatActivity {

    ListView fst;
    String[] placesname={"Place 1","Place 2","Place 3","Place 4","Place 5","Place 6"};
    String[] desc={"Desc 1","Desc 2","Desc 3","Desc 4","Desc 5","Desc 6"};
    Integer[] imgid={R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4,R.drawable.img5,R.drawable.img6};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fst= (ListView) findViewById(R.id.listview);
        CustomListview customListview=new CustomListview(this,placesname,desc,imgid);
        fst.setAdapter(customListview);
        fst.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                if(position==0) {
                    Intent myintent = new Intent(view.getContext(),touristspot1.class);
                    startActivityForResult(myintent,0);
                }
                if(position==1) {
                    Intent myintent = new Intent(view.getContext(),touristspot2.class);
                    startActivityForResult(myintent,1);
                }
            }
        });

    }
}

自定义列表视图

public class CustomListview extends ArrayAdapter<String> {

    private String[] placesname;
    private String[] desc;
    private Integer[] imgid;
    private Activity context;
    public CustomListview(Activity context, String[] placesname, String[] desc, Integer[] imgid) {
        super(context, R.layout.listview_layout, placesname);

        this.context = context;
        this.placesname = placesname;
        this.desc = desc;
        this.imgid = imgid;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View r=convertView;
            ViewHolder viewHolder=null;
            if (r==null)
            {

                LayoutInflater layoutInflater=context.getLayoutInflater();
                r=layoutInflater.inflate(R.layout.listview_layout,null,true);
                viewHolder=new ViewHolder(r);
                r.setTag(viewHolder);
            }
            else {

                viewHolder= (ViewHolder) r.getTag();
            }

            viewHolder.ivw.setImageResource(imgid[position]);
            viewHolder.tb1.setText(placesname[position]);
            viewHolder.tb2.setText(desc[position]);
            return r;

    }
    class ViewHolder
    {
        TextView tb1;
        TextView tb2;
        ImageView ivw;
        ViewHolder(View v)
        {
            tb1=v.findViewById(R.id.tubbaname);
            tb2=v.findViewById(R.id.tubbadesciption);
            ivw=v.findViewById(R.id.imageView);
        }
    }
}

我上面写的代码在我放入片段时不起作用。我下面写的代码是另一个项目,我把上面提到的代码放在这里

列表视图片段

PPTP

public class pptp extends Fragment {


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


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_pptp, container, false);
        // Inflate the layout for this fragment
        String[] awayStrings = {
                "Top Wonders",
                "Top Tourist Destinations",
                "Cities to Visit",
                "Romantic Places",
                "Family Destinations",
                "Best Honeymoon Destinations",
                "Adventure Sports Destinations",
                "Amusement Parks",
                "Best Beaches",
                "Monuments",
                "Historical Places",
                "National Parks and wild life sanctuary",
                "Places to Visit",
                "Beautiful Caves",
                "Best Cathedrals",
                "EcoNature Tourism",
                "Top Museums",
                "Beautiful Lakes",
                "Most Beautiful Sandbars",
                "Popular Street foods",
                "General Information",
        };

        ListView lv = (ListView) view.findViewById(R.id.listView);

        ArrayAdapter<String> lva = new ArrayAdapter<String>(
                getActivity(), android.R.layout.simple_list_item_1, awayStrings);
        lv.setAdapter(lva);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

                if(position == 0){
                    List1TW fragment = new List1TW();
                    FragmentTransaction transaction = getFragmentManager().beginTransaction();
                    transaction.replace(R.id.fragment_container, fragment);
                    transaction.addToBackStack(null);
                    transaction.commit();

                }
            }
        });
        return view;
    }

}

ViewPageAdapter

public class ViewPageAdapter extends FragmentPagerAdapter{

    ArrayList<Fragment> fragments = new ArrayList<>();
    ArrayList<String> tabTitles = new ArrayList<>();

    public void addFragments (Fragment fragments,String tabTitles){
        this.fragments.add(fragments);
        this.tabTitles.add(tabTitles);
    }

    public ViewPageAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return tabTitles.get(position);
    }
}

在 Fragment 中,我将如何在列表视图中查看图像?

【问题讨论】:

  • 显示你在“片段”中调用适配器的代码
  • 我已经写好了。它的 ViewPageAdapter
  • FragmentPagerAdapterArrayAdapter&lt;String&gt;有区别
  • 在 FragmentPagerAdapter 我应该怎么做?你能给我一些代码来查看列表视图片段中的图像吗?
  • pptp 是您要在其中显示的片段?

标签: android listview android-fragments


【解决方案1】:

问题是您在片段中使用简单的ArrayAdapter&lt;String&gt;,而在活动中您对图像使用自定义adapter

在你的片段中替换

ArrayAdapter<String> lva = new ArrayAdapter<String>(
            getActivity(), android.R.layout.simple_list_item_1, awayStrings);
    lv.setAdapter(lva);

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {

            if(position == 0){
                List1TW fragment = new List1TW();
                FragmentTransaction transaction = getFragmentManager().beginTransaction();
                transaction.replace(R.id.fragment_container, fragment);
                transaction.addToBackStack(null);
                transaction.commit();

            }
        }
    });

CustomListview customListview=new CustomListview(getActivity(),placesname,desc,imgid);
    lv.setAdapter(customListview);

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {

            if(position == 0){
                List1TW fragment = new List1TW();
                FragmentTransaction transaction = getFragmentManager().beginTransaction();
                transaction.replace(R.id.fragment_container, fragment);
                transaction.addToBackStack(null);
                transaction.commit();

            }
        }
    });

【讨论】:

  • 我会使用 customlistview 类吗?
  • 是的,您必须这样做,因为您使用的是简单的ArrayAdapter&lt;String&gt;,它仅用于显示字符串列表,因此当您更改布局时,即custom layout,您必须定义一个@987654327 @为它
  • 主Activity还是pptp片段我会用什么Activity?
  • 当然是你的 pptp 片段,你想在其中显示带有图像的列表
  • 我将添加另一个这个 Integer[] imgid={R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4,R.drawable.img5,R .drawable.img6};
猜你喜欢
  • 1970-01-01
  • 2013-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-31
  • 1970-01-01
相关资源
最近更新 更多