2016-12-13 65 views
0

我有一個應用程序將項目加載到FrameLayout內的ListView中。該部分工作正常,但click功能似乎不起作用,當我將鼠標懸停在項目上時,他們沒有可點擊的懸停外觀,點擊時什麼都不做。Android FrameLayout ListView元素不出現可點擊

我搜索了一下,有一些關於焦點和可點擊類型的提及,但嘗試添加這些他們似乎沒有任何區別。

我使用的佈局結構有什麼問題嗎?或者我可以添加一些東西來確保ListView元素是可點擊的?

LansFragment

Map<String,String> myMap = new HashMap<>(); 
JSONArray lanArray = data; //json arrray, blank for example 

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

    View view = inflater.inflate(R.layout.activity_main, container, false); 

    ArrayList<String> itemArray = new ArrayList<String>(); 

    try { 
     JSONArray jsonMainArr = jsonObj.getJSONArray(lanArray); 

     for (int i = 0; i < jsonMainArr.length(); i++) { 

      JSONObject json_data = jsonMainArr.getJSONObject(i); 

      String name = json_data.getString("lanName"); 
      String key = json_data.getString("lanKey"); 

      itemArray.add(name); 
      myMap.put(name, key); 
     } 
    } catch (Exception e) { 
     // catch here 
    } 

    ArrayAdapter adapter = new ArrayAdapter<String>(view.getContext(), R.layout.activity_listview, itemArray); 

    ListView listView = (ListView) getActivity().findViewById(R.id.listview); 
    listView.setAdapter(adapter); 

    listView.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> l, View v, int position, long id) { 

      String lan = l.getItemAtPosition(position).toString(); 
      String key = myMap.get(lan); 

      // Not currently working due to no click registering 
     } 
    }); 

    return view; 
} 

activity_main

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="app.stats.LansFragment" 
    android:orientation="horizontal"> 

    <FrameLayout 
     android:id="@+id/fragment_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

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

</LinearLayout> 

activity_listview

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/label" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:padding="10dip" 
    android:textSize="16dip" 
    android:textStyle="bold" > 
</TextView> 
+0

爲什麼即使有FrameLayout呢? –

+0

@CodyCaughlan我正在使用片段,但它會有太多的代碼演示在這裏,所以我已經遺漏了部分 – Dan

+1

你的調用listview id在你的片段中調用ListView listView =(ListView)getActivity()。findViewById R.id.listview);'它應該是'ListView listView =(ListView)view.findViewById(R.id.listview);' –

回答

2

您的CAL L時您來電片段內ListView ID:

ListView listView = (ListView) getActivity().findViewById(R.id.listview);

但是你的片段裏,你應該引用片段視圖。它應該是:

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