在我的应用程序中,我在Fragment中使用带有ListView的导航抽屉。我成功地在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>
答案 0 :(得分:1)
您必须制作自定义适配器,如BaseAdapter而不是arrayadapter,并将图像添加到drawable文件夹中。 查看BaseAdapter教程 http://theopentutorials.com/tutorials/android/listview/android-custom-listview-with-image-and-text-using-baseadapter/
答案 1 :(得分:0)
将图像插入drawable文件夹,然后将其链接到xml文件,使用<imageview>