【问题标题】:Add images in ListView in Fragment - Navigation Drawer在 Fragment 中的 ListView 中添加图像 - 导航抽屉
【发布时间】:2017-07-07 08:17:40
【问题描述】:

在我的应用程序中,我在 Fragment 中使用带有 ListView 的 Navigation Drawer。我成功地在 Fragment 中实现了列表视图。但现在我想在这个列表中添加图像。怎么做。需要一些例子

这是我的 Vegall.java 文件

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class Vegall extends Fragment {
    private ListView lv;
    private static String[] vegalllist = {"Vegetables1", "Vegetables2"};

    public static Vegall newInstance()
    {
        Vegall vegall =new Vegall();
        return vegall;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View rootView=inflater.inflate(R.layout.vegall, null);

        lv = (ListView) rootView.findViewById(R.id.vegall_lv);

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

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l){
                //Toast.makeText(getActivity(), vegcat[i], Toast.LENGTH_SHORT).show();
                selectItem(i);

            }
        });
        return rootView;
    }
    private void selectItem(int i){
        Fragment newFragment;
        FragmentTransaction transaction = getFragmentManager().beginTransaction();

        switch (i){
            case 0:
                newFragment = new Vegetables1();
                transaction.replace(R.id.containerID, newFragment);
                transaction.addToBackStack(null);
                transaction.commit();
                break;

            case 1:
                Intent intent = new Intent(getActivity(), Vegetables2.class);
                startActivity(intent);
                break;
        }
    }
    @Override
    public String toString(){
        return "Home";
    }

}

这是我的 Vegall.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ListView
        android:id="@+id/vegall_lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

【问题讨论】:

    标签: android listview navigation-drawer


    【解决方案1】:

    您必须制作自定义适配器,如 BaseAdapter 而不是 arrayadapter,并将图像添加到可绘制文件夹中。 查看 BaseAdapter 教程 http://theopentutorials.com/tutorials/android/listview/android-custom-listview-with-image-and-text-using-baseadapter/

    【讨论】:

      【解决方案2】:

      在drawable文件夹中插入图片,然后使用&lt;imageview&gt;链接到xml文件

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多