2

我在一個分片活動中有一個Viewpager,它有一個帶有編輯文本和發送按鈕的botton框架。 在片段佈局中,我有一個ListView,並在片段中附加了一個適配器。現在我正在實現從片段中的Parent的發送按鈕,點擊試圖通過適配器將編輯文本中的文本(再次從父文件)添加到列表(在片段中)。但這裏的問題是,當我輸入一些文本,然後單擊發送其添加文本不在當前瀏覽器中的視圖,但到下一個,當我去最後一個項目時,它添加到相同的視圖(這是預期的)。有時候它的隨機不是按照下一個項目的順序。 片段活動佈局:Android查看帶有列表視圖的分頁器並動態添加文本

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/detailLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:clickable="true" 
    android:orientation="vertical" > 

    <android.support.v4.view.ViewPager 
     android:id="@+id/pager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_above="@+id/form" > 
    </android.support.v4.view.ViewPager> 

    <RelativeLayout 
     android:id="@+id/form" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:orientation="horizontal" 
     android:padding="2dp" > 

     <EditText 
      android:id="@+id/editText" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_centerVertical="true" 
      android:layout_toLeftOf="@+id/btnSend" 
      android:drawableRight="@drawable/edit_keyboard" 
      android:inputType="textMultiLine" 
      android:maxLines="3" /> 

     <ImageButton 
      android:id="@+id/btnSend" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_centerVertical="true" 
      android:src="@drawable/send" /> 
    </RelativeLayout> 

</RelativeLayout> 

片段佈局:

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

    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <ListView 
     android:id="@+id/listView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@android:color/transparent" 
     android:cacheColorHint="@android:color/transparent" 
     android:divider="@android:color/transparent" 
     android:listSelector="@android:color/transparent" 
     android:scrollbars="none" /> 

</RelativeLayout> 

下面是我實現發送的ImageButton:

ImageButton sendBtn = (ImageButton) getActivity().findViewById(R.id.btnSend); 

sendBtn.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       String msg = msgBox.getText().toString(); 
       if (msg.length() > 0) { 
        long date = new Date().getTime(); 
        // TODO Auto-generated method stub 
        commentAdapter.add(new OneComment(false, msg, "Me", 
        String.valueOf(date), "0")); 

        msgBox.setText(""); 
       } else 
        Toast.makeText(getActivity(), "type in some text..", Toast.LENGTH_SHORT).show(); 
      } 
     }); 

回答

2

您可以訪問到當前片段(具有列表)做這樣的事情:

Fragment currentFragment = (Fragment) viewPager.getAdapter().instantiateItem(viewPager, viewPager.getCurrentItem()); //gets current fragment 
//now you have to cast it to your fragment with list, let's say it's name is SukarnoListFragment 

((SukarnoListFragment)currentFragment).messageList.add(...); // you have now access to public fields and methods that are inside your fragment 

我認爲如果你在Activity中有你的按鈕,那麼你應該在你的Activity中實現onClick這個按鈕,而不是在你的片段中。

+0

謝謝,我會試試這個!並回來..但最新情況發生在我的情況下,當我嘗試從片段添加它自我 – sukarno

+0

viewPager.getChild返回一個視圖,我不能將它投到一個Fargment。 :( – sukarno

+0

對不起我的錯誤,我沒有使用一段時間,我更新了代碼。 –

相關問題