Android 使用 ListView 查看分页器并动态添加文本

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

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

<?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>

fragment 布局:

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

最佳答案

您可以访问当前 fragment (带有列表的 fragment ),执行如下操作:

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

关于Android 使用 ListView 查看分页器并动态添加文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14701429/

相关文章:

android - 在 android 中快速滚动时避免滑动刷新

android - 在具有非静态 fragment 的布局上有效地使用布局编辑器

java - 如何从另一个函数获取java中的hashmap键和值

android - iOS能像安卓一样接收广播吗?

java - Android 中用于自定义布局的 ArrayAdapter?

android - 使用 EditText 搜索 ListView

java - onClick 通知 fragment Android

java - 将 ArrayList 传递给 fragment

Android 动画矢量绘图 : change at runtime

android - 如何使用点击监听器和后台 worker 更改 android View 的外观?