【问题标题】:Android View pager with listview and adding text dynamicallyAndroid View pager with listview和动态添加文本
【发布时间】:2013-02-05 06:25:17
【问题描述】:

我在 Fragment 活动中有一个 Viewpager,它有一个带有编辑文本和发送按钮的底部框架。 在片段布局中,我有一个 Listview 并在片段中附加了一个适配器。现在我正在从片段内的父级实现发送按钮,单击时我试图通过适配器将编辑文本中的文本(再次来自父级)添加到列表(在片段中)。但是这里的问题是,当我输入一些文本并单击发送时,将文本添加到寻呼机中的当前视图而不是下一个视图,并且当我有时转到最后一项时,它会添加到同一个视图(这是预期的)。有时它是随机的,而不是下一个项目的顺序。 片段活动布局:

<?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();
            }
        });

【问题讨论】:

    标签: android android-listview android-fragments android-viewpager android-fragmentactivity


    【解决方案1】:

    您可以通过以下方式访问当前片段(带有列表的片段):

    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,而不是在片段中。

    【讨论】:

    • 谢谢,我会试试这个!然后回来..但是在我的情况下发生了什么,当我尝试从片段中添加它自己时
    • viewPager.getChild 返回一个视图,但我无法将其投射到 Fargment。 :(
    • 对不起,我的错误,我有一段时间没用这个了。我更新了代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多