滑动ListItem + Pull-To-Refresh效果+ Click事件+垂直滚动

时间:2015-05-27 14:32:17

标签: java android pull-to-refresh swipe-gesture swiperefreshlayout

我需要一个带有垂直滚动的列表视图,允许“拉动 - 刷新”效果,每个单元格必须允许滑动(以显示下面的按钮)。此外,整个ListView包含在三个选项卡活动中。它与Android的Gmail应用程序非常相似,在收件箱中允许垂直滚动,滑动到存档对话,然后单击以阅读电子邮件。

  • 一般活动包含从developer.android.com中提取的标签(使用Horizo​​ntalScrollView,SlidingTabStrip和ViewPager构建的SlidingTabLayout)。
  • 每个标签包含一个带有FrameLayout的片段,里面有一个47degree SwipeListView。他们附在这里。
  • 我们已禁用标签的水平移动(它们只是点击标题
  • 我附上了单元格的布局和处理所有内容的java类。
  • 我实现了47deg SwipeList库(https://github.com/47deg/android-swipelistview)。

我面临的错误是:一方面,滑动的单元格没有接收到主要点击,当我滑动时,会显示按钮,但是没有正确接收到点击。如果我们启用pull-to-refresh,滑动就会停止运行。

layout_list_item_contact.xml:

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

    <RelativeLayout
        android:orientation="vertical"
        android:id="@+id/back"
        android:tag="back"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:background="@color/gold">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btFavorite"
            android:background="@drawable/abc_btn_rating_star_off_mtrl_alpha"
            android:layout_alignParentLeft="false"
            android:adjustViewBounds="true"
            android:contentDescription="Favorite"
            android:layout_centerVertical="true"
            android:layout_marginLeft="15dp" />
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/front"
        android:tag="front"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffffffff"
        android:layout_marginLeft="5dp">
    <include android:id="@+id/layout_list_item_image" layout="@layout/layout_list_item_image"
        android:layout_width="70dp"
        android:layout_height = "70dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />
    <include android:id="@+id/layout_list_item_content" layout="@layout/layout_list_item_content"
        android:layout_width="184dp"
        android:layout_toRightOf="@id/layout_list_item_image"
        android:layout_height="70dp"
        android:layout_centerHorizontal="true"
        />
    <include android:id="@+id/layout_list_item_status"
        layout="@layout/layout_list_item_status"
        android:layout_width="match_parent"
        android:layout_height = "wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"/>
    </LinearLayout>
</FrameLayout >

layout_fragment_pager_contact_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="false"
    >
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:clickable="false">

        <com.fortysevendeg.swipelistview.SwipeListView
            xmlns:swipe="http://schemas.android.com/apk/res-auto"
            android:id="@android:id/list"
            android:listSelector="#00000000"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            swipe:swipeFrontView="@+id/front"
            swipe:swipeBackView="@+id/back"
            swipe:swipeActionRight="reveal"
            swipe:swipeMode="right"
            swipe:swipeCloseAllItemsWhenMoveList="true"
            swipe:swipeOpenOnLongPress="false"
            swipe:swipeAnimationTime="350"
            swipe:swipeOffsetLeft="280dp"
            swipe:swipeOffsetRight="280dp"
            />

        <!-- View to show if the list is emtpy -->
        <TextView android:id="@android:id/empty"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="No items."
            android:clickable="false"/>

    </FrameLayout>

</LinearLayout>

ContactListFragment.java

package com.davduran.myapp.contacts.view;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcelable;
import android.support.v4.app.ListFragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.fortysevendeg.swipelistview.SwipeListView;
import com.davduran.myapp.R;
import com.davduran.myapp.contacts.connection.ContactController;
import com.davduran.myapp.contacts.detail.ContactDetailMainActivity;
import com.davduran.myapp.util.Constants;
import com.davduran.myapp.util.Utils;
import com.davduran.myapp.view.tab.SlidingTabLayout;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

import io.realm.Realm;
import model.Contact;
import model.FavouriteContact;
import model.RecentContact;

/**
 * A fragment representing a list of Items.
 * <p/>
 * <p/>
 * Activities containing this fragment MUST implement the {@link OnFragmentInteractionListener}
 * interface.
 */
public class ContactListFragment extends ListFragment {

    private SlidingTabLayout mSlidingTabLayout;
    private ViewPager mViewPager;
    private Realm realm;
    private ContactController mContactController;
    private ArrayList<Contact> contactList;
    private ArrayList<FavouriteContact> favouriteContactList;
    private ArrayList<RecentContact> recentContactList;
    protected Handler handler = new Handler();

    private ContactListViewArrayAdapter adapter;
    private SwipeListView swipeListView;
    private ListView listView;
    private Parcelable state;
    private TextView emptyText;

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private int mIndex;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

    // TODO: Rename and change types of parameters
    public static ContactListFragment newInstance(int index, String param2) {
        ContactListFragment fragment = new ContactListFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_PARAM1, index);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

   @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
       View v = inflater.inflate(R.layout.layout_fragment_pager_contact_list, container, false);
       listView = (ListView) v.findViewById(android.R.id.list);
       emptyText = (TextView) v.findViewById(android.R.id.empty);

       return v;
    }

    /**
     * Mandatory empty constructor for the fragment manager to instantiate the
     * fragment (e.g. upon screen orientation changes).
     */
    public ContactListFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        realm = Realm.getInstance(getActivity());
        mContactController = new ContactController(getActivity(),realm);
        if (getArguments() != null) {
            mIndex = getArguments().getInt(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }

        setListAdapterTabs();

    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnFragmentInteractionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Log.i(Constants.TAG, "ContactListFragment.onListItemClick: Listclicking");
        if (mListener != null) {
            // Notify the active callbacks interface (the activity, if the
            // fragment is attached to one) that an item has been selected.
            //mListener.onFragmentInteraction(DummyContent.ITEMS.get(position).getId());
            Intent in = new Intent(getActivity(), ContactDetailMainActivity.class);
            if(mIndex == Constants.CONTACTS_ALL) {
                in.putExtra(Constants.CONTACT_ID,contactList.get(position).getId() );
                startActivity(in);
            } else if (mIndex == Constants.CONTACTS_RECENT) {
                try {
                    String action = recentContactList.get(position).getAction();
                    if (action.compareTo("call") == 0) {
                        String strPhones = recentContactList.get(position).getPhones();
                        if (strPhones != null) {
                            JSONArray jPhones = new JSONArray(strPhones);
                            String phone = (String)((JSONObject) jPhones.get(0)).get("phone");
                            Utils.launchCall(phone, getActivity());
                        }
                    }
                    else if (action.compareTo("sms") == 0) {
                        String strPhones = recentContactList.get(position).getPhones();
                        if (strPhones != null) {
                            JSONArray jPhones = new JSONArray(strPhones);
                            String phone = (String)((JSONObject) jPhones.get(0)).get("phone");
                            Utils.launchSms(phone, getActivity());
                        }
                    }
                    else if (action.compareTo("email") == 0) {
                        String strEmails = recentContactList.get(position).getEmails();
                        if (strEmails != null) {
                            JSONArray jPhones = new JSONArray(strEmails);
                            String email = (String)((JSONObject) jPhones.get(0)).get("email");
                            Utils.launchEmail(email, getActivity());
                        }
                    }
                } catch (Exception ex) {
                    Log.e(Constants.TAG, "ContactListFragment.onListItemClick: ", ex);
                }
            } else if (mIndex == Constants.CONTACTS_FAVOURITE) { {
                in.putExtra(Constants.CONTACT_ID,favouriteContactList.get(position).getId() );
                startActivity(in);
            }}

        }
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p/>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        public void onFragmentInteraction(String id);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        realm.close();
    }

    public void setListAdapterTabs(){
        Log.i(Constants.TAG, "ContactListFragment.setListAdapterTabs: index " + mIndex);
        if(mIndex == Constants.CONTACTS_FAVOURITE) {
            favouriteContactList = mContactController.getAllFavouriteContacts();
            if (favouriteContactList!=null) {
                setListAdapter(new ContactFavouriteListViewArrayAdapter(getActivity().getApplicationContext(),
                        favouriteContactList));
            }
        }else if(mIndex == Constants.CONTACTS_RECENT){
            if (emptyText!=null)
                emptyText.setText("");
            recentContactList = mContactController.getAllRecentContacts();
            if (recentContactList!=null) {
               setListAdapter(new RecentListViewArrayAdapter(getActivity().getApplicationContext(), recentContactList));
            }
        }else if(mIndex == Constants.CONTACTS_ALL){
            if (emptyText!=null)
                emptyText.setText("");
            contactList = mContactController.getAllContacts();
            adapter = new ContactListViewArrayAdapter(getActivity().getApplicationContext(), contactList);
            if (contactList!=null) {
                if (listView!=null)
                    state = listView.onSaveInstanceState();
                if (adapter != null){
                    setListAdapter(adapter);
                    if (state!=null)
                        listView.onRestoreInstanceState(state);
                } else {
                    adapter = new ContactListViewArrayAdapter(getActivity().getApplicationContext(), contactList);
                    setListAdapter(adapter);
                    if (state!=null)
                        listView.onRestoreInstanceState(state);
                }
            }
        }
    }
}

0 个答案:

没有答案