Skip to content

Commit

Permalink
添加空布局设置功能
Browse files Browse the repository at this point in the history
  • Loading branch information
teach committed Apr 28, 2020
1 parent 31efeba commit 75e394c
Show file tree
Hide file tree
Showing 13 changed files with 284 additions and 19 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<activity android:name=".activity.VariousActivity" />
<activity android:name=".activity.VariousChildActivity" />
<activity android:name=".activity.BindingActivity" />
<activity android:name=".activity.EmptyActivity" />
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.donkingliang.groupedadapterdemo.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import com.donkingliang.groupedadapter.adapter.GroupedRecyclerViewAdapter;
import com.donkingliang.groupedadapter.holder.BaseViewHolder;
import com.donkingliang.groupedadapterdemo.R;
import com.donkingliang.groupedadapterdemo.adapter.EmptyAdapter;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
* 没有数据时,显示空布局。
*/
public class EmptyActivity extends AppCompatActivity {

private TextView tvTitle;
private RecyclerView rvList;
private EmptyAdapter adapter;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_group_list);

tvTitle = (TextView) findViewById(R.id.tv_title);
rvList = (RecyclerView) findViewById(R.id.rv_list);

tvTitle.setText(R.string.empty_list);

rvList.setLayoutManager(new LinearLayoutManager(this));
adapter = new EmptyAdapter(this, null);
// 显示空布局。默认不显示
adapter.showEmptyView(true);
adapter.setOnHeaderClickListener(new GroupedRecyclerViewAdapter.OnHeaderClickListener() {
@Override
public void onHeaderClick(GroupedRecyclerViewAdapter groupedAdapter, BaseViewHolder holder, int groupPosition) {
// 清空数据
adapter.clear();
}
});
adapter.setOnChildClickListener(new GroupedRecyclerViewAdapter.OnChildClickListener() {
@Override
public void onChildClick(GroupedRecyclerViewAdapter groupedAdapter, BaseViewHolder holder,
int groupPosition, int childPosition) {
Toast.makeText(EmptyActivity.this, "子项:groupPosition = " + groupPosition
+ ", childPosition = " + childPosition,
Toast.LENGTH_LONG).show();
}
});

rvList.setAdapter(adapter);
}

public static void openActivity(Context context) {
Intent intent = new Intent(context, EmptyActivity.class);
context.startActivity(intent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ protected void onCreate(Bundle savedInstanceState) {
findViewById(R.id.btn_various).setOnClickListener(this);
findViewById(R.id.btn_various_child).setOnClickListener(this);
findViewById(R.id.btn_binding_adapter).setOnClickListener(this);
findViewById(R.id.btn_binding_adapter).setOnClickListener(this);
findViewById(R.id.btn_empty_list).setOnClickListener(this);
}

@Override
Expand Down Expand Up @@ -69,6 +71,10 @@ public void onClick(View v) {
case R.id.btn_binding_adapter:
BindingActivity.openActivity(this);
break;

case R.id.btn_empty_list:
EmptyActivity.openActivity(this);
break;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.donkingliang.groupedadapterdemo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.donkingliang.groupedadapter.holder.BaseViewHolder;
import com.donkingliang.groupedadapterdemo.R;
import com.donkingliang.groupedadapterdemo.entity.GroupEntity;
import com.donkingliang.groupedadapterdemo.model.GroupModel;

import java.util.ArrayList;

/**
* 没有数据时,显示空布局
*/
public class EmptyAdapter extends GroupedListAdapter {

public EmptyAdapter(Context context, ArrayList<GroupEntity> groups) {
super(context, groups);
}

/**
* 返回false表示没有组尾
*
* @param groupPosition
* @return
*/
@Override
public boolean hasFooter(int groupPosition) {
return false;
}

/**
* 当hasFooter返回false时,这个方法不会被调用。
*
* @return
*/
@Override
public int getFooterLayout(int viewType) {
return 0;
}

@Override
public void onBindHeaderViewHolder(BaseViewHolder holder, int groupPosition) {
GroupEntity entity = mGroups.get(groupPosition);
holder.setText(R.id.tv_header, entity.getHeader() + "(点击清空数据)");
}

/**
* 当hasFooter返回false时,这个方法不会被调用。
*
* @param holder
* @param groupPosition
*/
@Override
public void onBindFooterViewHolder(BaseViewHolder holder, int groupPosition) {

}

@Override
public View getEmptyView(ViewGroup parent) {
View view = LayoutInflater.from(mContext).inflate(R.layout.adapter_empty_view, parent, false);
Button btnLoadData = view.findViewById(R.id.btn_load_data);
btnLoadData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setData();
}
});
return view;
}

private void setData() {
setGroups(GroupModel.getGroups(10, 5));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
public class GroupedListAdapter extends GroupedRecyclerViewAdapter {

private ArrayList<GroupEntity> mGroups;
protected ArrayList<GroupEntity> mGroups;

public GroupedListAdapter(Context context, ArrayList<GroupEntity> groups) {
super(context);
Expand All @@ -33,6 +33,16 @@ public int getChildrenCount(int groupPosition) {
return children == null ? 0 : children.size();
}

public void clear(){
mGroups.clear();
notifyDataChanged();
}

public void setGroups(ArrayList<GroupEntity> groups){
mGroups = groups;
notifyDataChanged();
}

@Override
public boolean hasHeader(int groupPosition) {
return true;
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@
android:layout_margin="5dp"
android:text="@string/binding_adapter"
android:textAllCaps="false"/>

<Button
android:id="@+id/btn_empty_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="@string/empty_list"
android:textAllCaps="false"/>
</LinearLayout>

</ScrollView>
Expand Down
36 changes: 36 additions & 0 deletions app/src/main/res/layout/adapter_empty_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?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="match_parent"
android:orientation="vertical">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/group_adapter_empty_view_image" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/empty_view"
android:textColor="@android:color/black"
android:textSize="18sp" />

<Button
android:id="@+id/btn_load_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/load_data"
android:textColor="@android:color/black"
android:textSize="18sp" />
</LinearLayout>


</FrameLayout>
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@
<string name="various">头、尾和子项都支持多种类型的列表</string>
<string name="various_child">多种子项类型的列表</string>
<string name="binding_adapter">使用DataBinding的列表</string>
<string name="empty_list">空布局</string>
<string name="empty_view">这是个空布局</string>
<string name="load_data">加载数据</string>

</resources>
5 changes: 0 additions & 5 deletions groupedadapter/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.donkingliang.groupedadapter">

<application
android:label="@string/app_name">

</application>

</manifest>
Loading

0 comments on commit 75e394c

Please sign in to comment.