2017-07-07 73 views
-5

在我的應用程序中,我使用的導航抽屜與片段中的ListView。我成功實現了Fragment中的列表視圖。但現在我想在這個列表中添加圖像。怎麼做。需要一些例子在片段的Lis​​tView中添加圖像 - 導航抽屜

這是我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

插入圖片,然後將其鏈接到XML文件使用<imageview>