Android中的Listfragment按钮显示

时间:2015-04-21 08:24:35

标签: android listview android-listfragment

我正在使用我正在使用列表片段的Android应用程序。我在布局的顶部添加了一个按钮。我的问题是它在每个列表选项中重复。我只是希望它只在INDIA选项列表的顶部显示一次。

我的快照代码如下:

public class CountryList extends ListFragment {


    Button btn1;
    // Array of strings storing country names
    String[] countries = new String[] {
            "India",
            "Pakistan",
            "Sri Lanka",
            "China",
            "Bangladesh",
            "Nepal",
            "Afghanistan",
            "North Korea",
            "South Korea",
            "Japan"
    };    

    // Array of integers points to images stored in /res/drawable/
    int[] flags = new int[]{
            R.drawable.india,
            R.drawable.pakistan,
            R.drawable.srilanka,
            R.drawable.china,
            R.drawable.bangladesh,
            R.drawable.nepal,
            R.drawable.afghanistan,
            R.drawable.nkorea,
            R.drawable.skorea,
            R.drawable.japan    
    };

    // Array of strings to store currencies
    String[] currency = new String[]{
        "Indian Rupee",
        "Pakistani Rupee",
        "Sri Lankan Rupee",
        "Renminbi",
        "Bangladeshi Taka",
        "Nepalese Rupee",
        "Afghani",
        "North Korean Won",
        "South Korean Won",
        "Japanese Yen"
    };


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


        // Each row in the list stores country name, currency and flag
        List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();        

        for(int i=0;i<10;i++){
            if(countries[i]=="India"){

            }
            HashMap<String, String> hm = new HashMap<String,String>();
            hm.put("txt", "Country : " + countries[i]);
            hm.put("cur","Currency : " + currency[i]);
            hm.put("flag", Integer.toString(flags[i]) );            
            aList.add(hm);        
        }

        // Keys used in Hashmap
        String[] from = { "flag","txt","cur" };

        // Ids of views in listview_layout
        int[] to = { R.id.flag,R.id.txt,R.id.cur};        

        // Instantiating an adapter to store each items
        // R.layout.listview_layout defines the layout of each item
        SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.listview_layout, from, to);       

        setListAdapter(adapter);

        return super.onCreateView(inflater, container, savedInstanceState);     
    }

}

我的XML代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"    
     >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Button" />

    <ImageView 
            android:id="@+id/flag"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:contentDescription="HELLO WORLD"
            android:layout_gravity="center"
            />

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

            <TextView 
                android:id="@+id/txt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="15dp" />          

            <TextView 
                android:id="@+id/cur"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="10dp" />

        </LinearLayout>

</LinearLayout>

enter image description here

2 个答案:

答案 0 :(得分:2)

我认为您已经为列表制作了自定义布局。

您正在使用SimpleAdapter。但是根据您的要求,您需要制作一个自定义适配器,您可以将其作为SimpleAdapterBaseAdapter实现。在该课程中,您会找到一种getView()方法,您可以在其中对自定义视图进行充气并检查以下条件。

  if(countries[position].eqauls("India")) {
      yourButton.setVisibility(View.VISIBLE);
  }else{
      yourButton.setVisibility(View.GONE);
  }

答案 1 :(得分:0)

而不是在row_layout中添加按钮..仅使用该按钮创建新的Xml ..将此布局添加为listView的headerView。 这将在列表视图的顶部添加按钮。 如果问题出现,请告诉我,任何! 干杯!

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


        // Each row in the list stores country name, currency and flag
        List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();        

        for(int i=0;i<10;i++){
            if(countries[i]=="India"){

            }
            HashMap<String, String> hm = new HashMap<String,String>();
            hm.put("txt", "Country : " + countries[i]);
            hm.put("cur","Currency : " + currency[i]);
            hm.put("flag", Integer.toString(flags[i]) );            
            aList.add(hm);        
        }

        // Keys used in Hashmap
        String[] from = { "flag","txt","cur" };

        // Ids of views in listview_layout
        int[] to = { R.id.flag,R.id.txt,R.id.cur};        

        // Instantiating an adapter to store each items
        // R.layout.listview_layout defines the layout of each item
        SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.listview_layout, from, to);       
View headerView;
 headerView = inflater.inflate(R.layout.list_header, null,false);
 getListView().addHeaderView(mHeaderView);
        setListAdapter(adapter);

        return super.onCreateView(inflater, container, savedInstanceState);     
    }