2012-10-01 49 views
5

我創建了一個應用程序,其中有兩個片段,兩個片段中都有listviews。 fragment1中的第一個listview正在滾動,並且項目也被突出顯示。但在第二個片段中,listview不會滾動,甚至項目也不會突出顯示。有人能告訴我什麼是問題嗎?這裏的東西是我只是檢查了這一點,把相同的片段類放入xml中的兩個片段。要麼他們都應該工作,要麼兩者都不應該,因爲一個人與另一個人沒有區別。但爲什麼會出現這個問題?listfragment中的第二個listview不滾動

我的片段類:

public class Fragment1 extends ListFragment{ 

    String[] countries = new String[] { 
     "India", 
     "Pakistan", 
     "Sri Lanka", 
     "China", 
     "Bangladesh", 
     "Nepal", 
     "Afghanistan", 
     "North Korea", 
     "South Korea", 
     "Japan" 
    }; 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    { 
     return inflater.inflate(R.layout.fragment1,container,false); 
    } 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,countries); 
     setListAdapter(adapter); 
    } 

    public void onListItemClick(ListView parent, View v,int position, long id) 
    { 
     Toast.makeText(getActivity(), "You have selected "+countries[position], Toast.LENGTH_SHORT).show(); 
    } 

} 

的main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" > 

    <fragment 
     android:name="com.example.listfragmentexample.Fragment1" 
     android:id="@+id/fragment1" 
     android:layout_weight="0.5" 
     android:layout_width="0dp" 
     android:layout_height="200dp" /> 

    <fragment 
     android:name="com.example.listfragmentexample.Fragment1" 
     android:id="@+id/fragment2" 
     android:layout_weight="0.5" 
     android:layout_width="0dp" 
     android:layout_height="300dp"/> 
</LinearLayout> 

fragment1.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/android:list" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:drawSelectorOnTop="false"/> 

</LinearLayout> 

回答

3

所以,按你的代碼,似乎你是指的在你的main.xml中同樣有Fragment1 class。我假設您的活動類只包含onCreate()方法中的setContentView()。由於這兩個片段都在一個單獨的活動中,所以最初只有一個視圖可能會突出顯示。我剛剛檢查過,但它工作正常。相當於你可能一直在滾動它。如果你想讓你的第二個列表視圖突出顯示,恐怕你可能需要單獨的xml文件(比如fragment1和fragment2)以及單獨的片段類,然後通過添加下面的代碼來首先獲得關注點。

listView1 = (ListView)findViewById(R.id.listView1); 
listView1.requestFocus(); 

祝你好運。

+0

是的。謝謝。當我嘗試滾動第二個列表視圖時,即使將光標置於其上並單擊其項目,它也不會滾動。所以,我感到困惑,但拖動作品。沒關係。好吧,會嘗試實施您的建議,以突出顯示另一個。 – Korhan

相關問題