compile 'com.github.Yalantis:pull-to-make-soup:1.0.2'
maven { url 'https://jitpack.io' }
布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="dy.com.btn_my.MainActivity"> <com.yalantis.pulltomakesoup.PullToRefreshView android:id="@+id/pull_to_refresh" app:type="soup" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" android:divider="@null" android:dividerHeight="0dp" android:fadingEdge="none"/> </com.yalantis.pulltomakesoup.PullToRefreshView> </RelativeLayout>
子布局
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="@dimen/list_item_height" android:background="@color/eggplant"> <ImageView android:id="@+id/image_view_icon" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </FrameLayout>
public class MainActivity extends AppCompatActivity { private PullToRefreshView mPullToRefreshView; private static final int REFRESH_DELAY = 4000; private static final String KEY_ICON = "icon"; private static final String KEY_COLOR = "color"; private List<Map<String, Integer>> mSampleList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Map<String, Integer> map; mSampleList = new ArrayList<>(); int[] icons = { R.drawable.burger, R.drawable.pizza}; int[] colors = { R.drawable.rounded_background_burger, R.drawable.rounded_background_pizza, R.drawable.rounded_background_burger, R.drawable.rounded_background_pizza}; for (int i = 0; i < icons.length; i++) { map = new HashMap<>(); map.put(KEY_ICON, icons[i]); map.put(KEY_COLOR, colors[i]); mSampleList.add(map); } RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view); recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerView.setAdapter(new SampleAdapter()); mPullToRefreshView = (PullToRefreshView) findViewById(R.id.pull_to_refresh); mPullToRefreshView.setOnRefreshListener(new PullToRefreshView.OnRefreshListener() { @Override public void onRefresh() { mPullToRefreshView.postDelayed(new Runnable() { @Override public void run() { mPullToRefreshView.setRefreshing(false); } }, REFRESH_DELAY); } }); } private class SampleAdapter extends RecyclerView.Adapter<SampleHolder> { @Override public SampleHolder onCreateViewHolder(ViewGroup parent, int pos) { View view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.list_item, parent, false); return new SampleHolder(view); } @Override public void onBindViewHolder(SampleHolder holder, int pos) { Map<String, Integer> data = mSampleList.get(pos); holder.bindData(data); } @Override public int getItemCount() { return mSampleList.size(); } } private class SampleHolder extends RecyclerView.ViewHolder { private final View mRootView; private final ImageView mImageViewIcon; private Map<String, Integer> mData; public SampleHolder(View itemView) { super(itemView); mRootView = itemView; mImageViewIcon = (ImageView) itemView.findViewById(R.id.image_view_icon); } public void bindData(Map<String, Integer> data) { mData = data; mRootView.setBackgroundResource(mData.get(KEY_COLOR)); mImageViewIcon.setImageResource(mData.get(KEY_ICON)); } } }