android - 如何下拉刷新 ListView 而不重复它的项目

标签 android listview

我创建了一个应用程序来从网络获取 rss 提要并将其显示在 ListView 上,我想向下滑动(拉)以刷新 ListView 项目,这是我的第一个问题,第二个是当我向下滚动列表时,如果我在 listview 上向上滚动,它会开始刷新吗? 我的主要 activity.java :

public class MainActivity extends AppCompatActivity {
private ListView listView;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    if (savedInstanceState == null) {
        addRssFragment();
    }
    listView=(ListView)findViewById(R.id.listView);
    final SwipeRefreshLayout pullToRefresh = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
    pullToRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            addRssFragment();
            pullToRefresh.setRefreshing(false);
        }
    });
}

private void addRssFragment() {
    FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    RssFragment fragment = new RssFragment();
    transaction.add(R.id.fragment_container, fragment);
    transaction.commit();
}
}

我的 RssFragment.java :

public class RssFragment extends Fragment implements OnItemClickListener {

private ProgressBar progressBar;
private ListView listView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_layout, container, false);
    progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
    listView = (ListView) view.findViewById(R.id.listView);
    listView.setOnItemClickListener(this);
    return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    startService();
}

private void startService() {
    Intent intent = new Intent(getActivity(), RssService.class);
    getActivity().startService(intent);
}

/**
 * Once the {@link RssService} finishes its task, the result is sent to this BroadcastReceiver
 */
private BroadcastReceiver resultReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        progressBar.setVisibility(View.GONE);
        List<RssItem> items = (List<RssItem>) intent.getSerializableExtra(RssService.ITEMS);
        if (items != null) {
            RssAdapter adapter = new RssAdapter(getActivity(), items);
            listView.setAdapter(adapter);
        } else {
            Toast.makeText(getActivity(), "An error occurred while downloading the rss feed.",
                    Toast.LENGTH_LONG).show();
        }
    }
};

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    RssAdapter adapter = (RssAdapter) parent.getAdapter();
    RssItem item = (RssItem) adapter.getItem(position);
    Uri uri = Uri.parse(item.getLink());
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    startActivity(intent);
}

第二期: fragment 布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/listView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
</ListView>

<ProgressBar
    android:id="@+id/progressBar"
    style="?android:attr/progressBarStyleLarge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true" />

</RelativeLayout>

主要内容:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent">

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:id="@+id/fragment_container"
android:layout_height="fill_parent" />

</android.support.v4.widget.SwipeRefreshLayout>

最佳答案

首先,您应该在 fragment_layout 下添加 SwipeRefreshLayout,这是您的 ListView 。

这就是您的fragment_layout 的样子

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

     <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_container"
        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" >
        </ListView>

     </android.support.v4.widget.SwipeRefreshLayout>

</RelativeLayout>

主要布局。

<?xml version="1.0" encoding="utf-8"?>

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:id="@+id/fragment_container"
    android:layout_height="match_parent" />

从 fragment 中删除 onActivityCreated 方法并替换下面的 onCreateView 方法

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_layout, container, false);
    progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
    listView = (ListView) view.findViewById(R.id.listView);
    listView.setOnItemClickListener(this);
    final SwipeRefreshLayout pullToRefresh = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
    pullToRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            startService();
            pullToRefresh.setRefreshing(false);
        }
    });
    return view;
}

关于android - 如何下拉刷新 ListView 而不重复它的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51921093/

相关文章:

c# - 为什么 ListView 的 OnDrawItem 事件不影响设计时环境?

c# - ListViewItem 的组未通过另一个集合保留

android - gradle:Android Studio 继承 buildtype

android - 设置自定义字体

java - 如何在属性中显示json数据

android - 如何避免列表之间出现双边框?

android - 您的内容必须有一个 id 属性为 ' android.r.id.list ' 的 ListView 。

java - Android 位置权限代码无法正常工作

java - 连接android时socket.io发送一些数据到服务器

Android inline-block 元素无法识别的字符问题