Android - 在标签片段中拉动以刷新

时间:2014-03-23 20:19:14

标签: android android-fragments android-listview

我尝试进行刷新,同时可以在列表视图的顶部向下滑动。我跟着this link使它工作,它在一个激活上。但我需要在主要活动中标记的片段上,所以我不能如示例所示在我的片段上使用此方法。

使用活动时,这里有正确的代码:

TestActivity.java

import java.util.ArrayList;
import java.util.List;

import uk.co.senab.actionbarpulltorefresh.library.ActionBarPullToRefresh;
import uk.co.senab.actionbarpulltorefresh.library.PullToRefreshLayout;
import uk.co.senab.actionbarpulltorefresh.library.listeners.OnRefreshListener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Fragment;
import android.app.ListFragment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

public class TestActivity extends BaseActivity {

    @Override
    protected Fragment getSampleFragment() {
        return new SimpleListFragment();
    }

    public static class SimpleListFragment extends ListFragment implements
            OnRefreshListener {

        int i = 0;

        private PullToRefreshLayout mPullToRefreshLayout;

        ArrayAdapter<String> adapter;

        List<String> list;

        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onViewCreated(view, savedInstanceState);

            list = new ArrayList<String>();
            int no = 1;
            for (int i = 0; i < 5; i++) {
                list.add("Item No :" + no++);
            }

            super.onViewCreated(view, savedInstanceState);
            ViewGroup viewGroup = (ViewGroup) view;

            // As we're using a ListFragment we create a PullToRefreshLayout
            // manually
            mPullToRefreshLayout = new PullToRefreshLayout(
                    viewGroup.getContext());

            // We can now setup the PullToRefreshLayout
            ActionBarPullToRefresh
                    .from(getActivity())
                    // We need to insert the PullToRefreshLayout into the
                    // Fragment's ViewGroup
                    .insertLayoutInto(viewGroup)
                    // Here we mark just the ListView and it's Empty View as
                    // pullable
                    .theseChildrenArePullable(android.R.id.list,
                            android.R.id.empty).listener(this)
                    .setup(mPullToRefreshLayout);

        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onActivityCreated(savedInstanceState);

            adapter = new ArrayAdapter<String>(getActivity(),
                    android.R.layout.simple_list_item_1, list);

            // Set the List Adapter to display the sample items
            setListAdapter(adapter);
            setListShownNoAnimation(true);
        }

        @Override
        public void onRefreshStarted(View view) {
            // TODO Auto-generated method stub

            // setListShown(false); // This will hide the listview and visible a
            // round progress bar

            new AsyncTask<Void, Void, Void>() {

                @Override
                protected Void doInBackground(Void... params) {
                    try {
                        Thread.sleep(5000); // 5 seconds
                        int itemNo = list.size();
                        itemNo++;
                        list.add("New Item No :" + itemNo);

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    return null;
                }

                @Override
                protected void onPostExecute(Void result) {
                    super.onPostExecute(result);

                    adapter.notifyDataSetChanged();
                    // Notify PullToRefreshLayout that the refresh has finished
                    mPullToRefreshLayout.setRefreshComplete();

                    // if you set the "setListShown(false)" then you have to
                    // uncomment the below code segment

                    // if (getView() != null) {
                    // // Show the list again
                    // setListShown(true);
                    // }
                }
            }.execute();

        }

    }

}

我已经多次试过让这个方法在我的片段代码中工作但是我不能成功。下面的代码是一个工作片段示例,没有任何TestActivity输入。

InfoFragment.java

import java.util.ArrayList;
import java.util.List;

import com.example.app.Config;
...
import android.app.Activity;
import android.os.Bundle;
import android.provider.Settings.Secure;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

public class TicketInfoFragment extends Fragment {

    List<Info> ticketInfo;
    TicketFull ticket = new TicketFull();
    private DatabaseHelper db;
    int ticketId = TicketActivity.getCurrentTicketId();
    String androidId;
    String authCode;
    String platform_url;
    int uId;
    View rootView;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        db = new DatabaseHelper(getActivity());
        androidId = Secure.getString(getActivity().getContentResolver(),
                Secure.ANDROID_ID);
        Config config = new Config();
        config = db.getConfig(androidId);
        authCode = config.getAuthCode();
        platform_url = config.getPlatformURL();
        uId = config.getuId();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_ticket_information, container, false);
        fillTicket();
        return rootView;
    }

    private void fillTicket() {
        HttpReader httpReader = new HttpReader();
        httpReader
                .setOnResultReadyListener(new HttpReader.OnResultReadyListener() {
                    @Override
                    public void resultReady(String result) {
                        JsonHelper jsonHelper = new JsonHelper();
                        ticket = jsonHelper.getTicket(result);
                        //showTicket();
                        fillSettings();
                    }
                });
        httpReader
                .execute("http://aa.domainlink.com:1324/example/API/v1/json.php?auth=example&a=ticket&uauth="
                        + authCode + "&uid=" + uId + "&id=" + ticketId);

    }

    private void readSettings() {
        InfoAdapter infoAdapter = new InfoAdapter(
                getActivity(), ticketInfo);
        final ListView listViewInfo = (ListView) rootView.findViewById(R.id.listViewInfo);
        listViewInfo.setAdapter(infoAdapter);

        listViewInfo
                .setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parentView,
                            View childView, int position, long id) {
                    }
                });
        listViewInfo.setEmptyView(rootView.findViewById(R.id.empty));
    }

    private void fillSettings() {
        String dueDate = getString(R.string.noDueDate);
        ticketInfo = new ArrayList<Info>();
        ticketInfo.add(new Info(getString(R.string.ticket_subject), ticket.getSubject() + ""));
        ticketInfo.add(new Info(getString(R.string.ticket_relation), ticket.getRelation() + ""));
        ticketInfo.add(new Info(getString(R.string.ticket_status), ticket.getStatus() + ""));
        ticketInfo.add(new Info(getString(R.string.ticket_priority), ticket.getPriority() + ""));
        ticketInfo.add(new Info(getString(R.string.ticket_created), ticket.getTicketCreate() + ""));
        ticketInfo.add(new Info(getString(R.string.ticket_department), ticket.getDepartmentName() + ""));
        ticketInfo.add(new Info(getString(R.string.ticket_user), ticket.getUser() + ""));
        if (!ticket.getDueDate().equals("00-00-0000 00:00:00")) {
            dueDate = ticket.getDueDate();
        }
        ticketInfo.add(new Info(getString(R.string.ticket_dueDate), dueDate));
        readSettings();
    }
}

活动通常需要的BaseActivity:

BaseActivity.java

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class BaseActivity extends Activity {

      @Override
      protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);

        // Add the Sample Fragment if there is one
        Fragment sampleFragment = getSampleFragment();
        if (sampleFragment != null) {
            getFragmentManager().beginTransaction()
                    .replace(android.R.id.content, sampleFragment).commit();
        }

      }

      //This method will override by child class. Then base class can get the fragment
      protected Fragment getSampleFragment() {
        return null;
    }

}

发布我在片段中实现活动方法的失败尝试不起作用我想我不会发布它们。

如果你们中的任何人都可以帮助,请多多谢谢。

1 个答案:

答案 0 :(得分:1)

Android SDK最终添加了SwipeRefreshLayout。也许你可以将它添加到你的布局? http://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html