2016-02-05 71 views
0

編輯:感謝@ankit aggarwal,我得到了一部分工作,即TAB 1無法切換到TAB 2。需要援助的觀點不刷新TAB 2中。無法使用swipable視圖獲取Android標籤,以正確地將信息從一個標籤頁傳遞到另一個標籤頁並正確顯示


我正在使用一個應用程序,其中我使用選項卡。我基於我在互聯網上找到的一個示例基於我的應用程序here

我所擁有的是TAB中的ListView 1.我想要發生的是當我單擊列表中的某個項目時,它將發送一個ID到TAB 2,在那裏我想根據傳遞的填充另一個ListView數據。我有問題讓這個工作正常。

目前,我的工作原理是,如果單擊TAB 1上列表中的項目,然後TAB 2會獲取數據並填充新列表,但它會添加到TAB 2上的視圖而不是刷新/覆蓋/等,並且該應用程序不會自動切換到新標籤。這是我在TAB 2(即接收標籤)的佈局:與

person.xml

<LinearLayout android:id="@+id/person_information_layout"> 
    <TextView android:id="@+id/header_row" /> 
    <LinearLayout> 
     <TextView android:id="@+id/column1_row_header" /> 
     <TextView android:id="@+id/column2_row_header" /> 
     <TextView android:id="@+id/column3_row_header" /> 
    </LinearLayout> 
    <ListView android:id="@+row_of_data"></ListView> 
</LinearLayout> 

我填充這個名單在接收選項卡(TAB 2) AdapterView和數據庫光標。

PersonInformation.java

package myPackage; 

public class PersonInformation extends Fragment { 

    private View rootView; 
    private DatabaseHelper myDBHelper; 
    private Cursor informationCursor; 
    private SimpleCursorAdapter mySimpleCursorAdapter; 

    private TextView personIDDisplay; 

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

     rootView = inflater.inflate(R.layout.person, container, false); 

     personID = getArguments().getString("personID"); 

     myDBHelper = new DatabaseHelper(getActivity()); 

     informationCursor = myDBHelper.getInformationCursor(personID); 
     String[] fromColumns = {"firstname", "lastname", "homephone", "homeaddress"}; 
     int[] toViews = {R.id.firstname_textview, R.id.lastname_textview, R.id.homephone_textview, R.id.homeaddress_textview}; 
     mySimpleCursorAdapter = new SimpleCursorAdapter(getActivity(), R.layout.person_information, informationCursor, fromColumns, toViews, 0); 

     personIDDisplay = (TextView) rootView.findViewById(R.id.header_row); 
     personIDDisplay.setText("Person ID: " + personID); 

     ListView myListView = (ListView) rootView.findViewById(R.id.row_of_data); 
     myListView.setAdapter(mySimpleCursorAdapter); 

     myDBHelper.close(); 

     return rootView; 
    } 
} 

這是person_information佈局被填充:

person_information.xml

<LinearLayout> 
     <TextView android:id="@+id/column1_data" /> 
     <TextView android:id="@+id/column2_data" /> 
     <TextView android:id="@+id/column3_data" /> 
</LinearLayout> 

,這裏是我的發送選項卡(TAB 1 )

Home.java

package myPackage; 

public class Home extends Fragment { 

    private View rootView; 
    private DatabaseHelper myDBHelper; 
    private Cursor personCursor; 
    private SimpleCursorAdapter mySimpleCursorAdapter; 
    private Activity myActivity; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ 
     rootView = inflater.inflate(R.layout.home, container, false); 

     myDBHelper = new DatabaseHelper(getActivity()); 

     personCursor = myDBHelper.getPersonCursor(); 
     String[] fromColumns = {"personID"}; 
     int[] toViews = {R.id.person_id_textview}; 
     mySimpleCursorAdapter = new SimpleCursorAdapter(getActivity(), R.layout.person_layout, personCursor, fromColumns, toViews, 0); 

     ListView myListView = (ListView) rootView.findViewById(R.id.person_row); 

     myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       Cursor subCursor = (Cursor) mySimpleCursorAdapter.getItem(position); 
       String personIDNumber = subCursor.getString(subCursor.getColumnIndex("personID")); 

       PersonInformation personInformation = new PersonInformation(); 
       Bundle myBundle = new Bundle(); 
       myBundle.putString("personID", personIDNumber); 
       personInformation.setArguments(myBundle); 
       getFragmentManager().beginTransaction().replace(R.id.person_information_layout, personInformation).commit(); 
      } 
     }); 

     myListView.setAdapter(mySimpleCursorAdapter); 

     myDBHelper.close(); 

     return rootView; 
    } 
} 

正如你所看到的,我有一個頂級的信息排佈局,然後列標題行,則數據行。我想是這樣的:

Top Information Row 
Column Headers 
data row 1 
data row 2 
... 
data row n 

目前,當我點擊TAB 1人,TAB 2確實得到TAB 1中的數據,並正確地填充一個新的列表,但它在吸引它已經存在的頂部信息下行和列標題,所以我有兩層:

Top Information Row 
Column Headers 
Top Information Row 
Column Headers 
data row 1 
data row 2 
... 
data row n 

而且,標籤不會自動地從TAB 1,當我在TAB 1點擊行切換到TAB 2。

而是這條線在發送選項卡(TAB 1)

getFragmentManager().beginTransaction().add(R.id.person_information_layout, personInformation).commit(); 

我改成了這一行:

getFragmentManager().beginTransaction().add(R.id.row_of_data, personInformation).commit(); 

但我得到的錯誤:

"addView(View) not supported in AdapterView" 

據我所知,我不能將視圖添加到ListView,所以我將它改回到ListView的父級,但是我得到重複的vi EW。

我繼續回來想從

ListView myListView = (ListView) rootView.findViewById(R.id.row_of_data); 

在我的接收舌(TAB 2)行更改爲這個

LinearLayout myLinearLayout = (LinearLayout) rootView.findViewById(R.id.person_information_layout); 

但光標適配器將不會與工作。

我需要做些什麼才能使這個工作?

回答

0

(1)

Currently, when I click the person in TAB 1, TAB 2 does get the data from TAB 1 and populates a new list correctly, however it draws it under the already existing top information row and column headers, so I have layers:

它發生,因爲這種說法而沒有切換到TAB2,它加入了新的片段上的活動的框架之上。

getFragmentManager()。beginTransaction()。add(R.id.person_information_layout,personInformation).commit();

(2)

Also, the tab does not automatically switch from TAB 1 to TAB 2 when I click a row in TAB 1.

您正在使用該標籤實施viewpager。因此,要改變TAB2,你可以做

viewPager.setCurrentItem(2); 

注意:你所缺少的這裏是一個視圖尋呼機自動加載一個頁面上一個和下一個到特定的頁面。所以當你在tab1中時,tab2已經被加載了。所以當你點擊列表項時,你的tab2不會刷新,因爲它已經加載了。

+0

我試過beginTransaction.replace(),但它並沒有替代它。 – Brian

+0

這是因爲您正在另一個視圖的頂部添加新視圖。雖然這不是實現你所嘗試的正確方法,但你可以使用replace來代替add()。並確保將佈局的寬度和高度設置爲匹配父級,如果這仍然將父級佈局設置爲背景 –

+0

就像我說的,我試過「替換」,但結果是一樣的。 – Brian

0

我解決類似問題的方法是通過創建從活動到片段的回調方法和從片段到活動的回調方法。當我點擊fragment1列表中的一行時,我將信息保存在該片段的參數中,並調用該活動的回調函數。在該函數中,我檢索傳遞的信息並將其加載到fragment2的參數中,並調用fragment2的回調函數。在那裏我檢索傳遞的信息並更新fragment2的列表。然後我在該活動中調用tabLayout.getTabAt(newPosition)來重新顯示fragment2的頁面。您還必須確保,由於片段的視圖在頁面之間移動時會被破壞,因此要檢查片段中列表是否仍在填充列表,以及是否還原列表中的項目。

相關問題