Michael Joseph
Michael Joseph

Reputation: 47

How to pull down to refresh listview without duplicate it's items

I have created an app to get rss feed from web and show it on listview , I want to swipe(pull) down to refresh listview items , this is my first problem the second is when while I'm scrolling down the list , if I scroll up on listview it start refreshing? my main 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();
}
}

my 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);
}

for second issue : 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: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>

main :

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

Upvotes: 1

Views: 211

Answers (1)

Krishna Sharma
Krishna Sharma

Reputation: 2877

First you should add SwipeRefreshLayout under fragment_layout where is your listview.

This is how your fragment_layout will look like

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

main layout.

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

Remove onActivityCreated method from your fragment and replace below onCreateView method

@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;
}

Upvotes: 1

Related Questions