Skip to content

add checked option for list item #153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
197 changes: 127 additions & 70 deletions example/src/main/java/com/cocosw/bottomsheet/example/ListAcitivty.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
import android.widget.AdapterView;
import android.widget.ArrayAdapter;

import com.cocosw.bottomsheet.BottomSheet;
import com.cocosw.bottomsheet.BottomSheetHelper;
import com.cocosw.query.CocoQuery;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.drawable.RoundedBitmapDrawable;
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory;

import com.cocosw.bottomsheet.BottomSheet;
import com.cocosw.bottomsheet.BottomSheetHelper;
import com.cocosw.query.CocoQuery;

/**
* Project: gradle
* Created by LiaoKai(soarcn) on 2014/9/22.
Expand All @@ -45,17 +45,28 @@ protected void onCreate(Bundle savedInstanceState) {
getSupportActionBar().setDisplayShowHomeEnabled(true);
q = new CocoQuery(this);
setTitle(getIntent().getStringExtra("title"));
String[] items = new String[]{"Janet Perkins", "Mary Johnson", "Peter Carlsson", "Trevor Hansen", "Aaron Bennett"};
String[] items = new String[]{
"Janet Perkins",
"Mary Johnson",
"Peter Carlsson",
"Trevor Hansen",
"Aaron Bennett"
};
q.id(R.id.listView)
.adapter(adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, android.R.id.text1, items))
.adapter(adapter = new ArrayAdapter<>(
this,
android.R.layout.simple_list_item_1,
android.R.id.text1,
items))
.itemClicked(this);
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home)
if (item.getItemId() == android.R.id.home) {
finish();
}
return super.onOptionsItemSelected(item);
}

Expand All @@ -69,11 +80,23 @@ private Drawable getRoundedBitmap(int imageId) {
Bitmap src = BitmapFactory.decodeResource(getResources(), imageId);
Bitmap dst;
if (src.getWidth() >= src.getHeight()) {
dst = Bitmap.createBitmap(src, src.getWidth() / 2 - src.getHeight() / 2, 0, src.getHeight(), src.getHeight());
dst = Bitmap.createBitmap(
src,
src.getWidth() / 2 - src.getHeight() / 2,
0,
src.getHeight(),
src.getHeight());
} else {
dst = Bitmap.createBitmap(src, 0, src.getHeight() / 2 - src.getWidth() / 2, src.getWidth(), src.getWidth());
dst = Bitmap.createBitmap(
src,
0,
src.getHeight() / 2 - src.getWidth() / 2,
src.getWidth(),
src.getWidth());
}
RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), dst);
RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(
getResources(),
dst);
roundedBitmapDrawable.setCornerRadius(dst.getWidth() / 2);
roundedBitmapDrawable.setAntiAlias(true);
return roundedBitmapDrawable;
Expand All @@ -85,96 +108,131 @@ private Drawable getRoundedBitmap(int imageId) {
protected Dialog onCreateDialog(final int position, Bundle args) {
switch (action) {
case 0:
sheet = new BottomSheet.Builder(this).icon(getRoundedBitmap(R.drawable.icon)).title("To " + adapter.getItem(position)).sheet(R.menu.list).listener(new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ListAcitivty.this.onClick(adapter.getItem(position), which);
}
}).build();
sheet = new BottomSheet.Builder(this).icon(getRoundedBitmap(R.drawable.icon))
.title("To " + adapter.getItem(position))
.sheet(R.menu.list)
.listener(new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ListAcitivty.this.onClick(adapter.getItem(position), which);
}
})
.build();
break;
case 1:
sheet = new BottomSheet.Builder(this).sheet(R.menu.noicon).listener(new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ListAcitivty.this.onClick(adapter.getItem(position), which);
}
}).build();
sheet = new BottomSheet.Builder(this).sheet(R.menu.noicon)
.listener(new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ListAcitivty.this.onClick(adapter.getItem(position), which);
}
})
.build();
break;

case 2:
sheet = new BottomSheet.Builder(this).darkTheme().title("To " + adapter.getItem(position)).sheet(R.menu.list).listener(new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ListAcitivty.this.onClick(adapter.getItem(position), which);
}
}).build();
sheet = new BottomSheet.Builder(this).darkTheme()
.title("To " + adapter.getItem(position))
.sheet(R.menu.list)
.listener(new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ListAcitivty.this.onClick(adapter.getItem(position), which);
}
})
.build();
break;
case 3:
sheet = new BottomSheet.Builder(this).sheet(R.menu.list).listener(new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ListAcitivty.this.onClick(adapter.getItem(position), which);
}
}).grid().build();
sheet = new BottomSheet.Builder(this).sheet(R.menu.list)
.listener(new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ListAcitivty.this.onClick(adapter.getItem(position), which);
}
})
.grid()
.build();
break;
case 4:
sheet = new BottomSheet.Builder(this, R.style.BottomSheet_StyleDialog).title("To " + adapter.getItem(position)).sheet(R.menu.list).listener(new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ListAcitivty.this.onClick(adapter.getItem(position), which);
}
}).build();
sheet = new BottomSheet.Builder(
this,
R.style.BottomSheet_StyleDialog).title("To " + adapter.getItem(position))
.sheet(R.menu.list)
.listener(new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ListAcitivty.this.onClick(adapter.getItem(position), which);
}
})
.build();
break;
case 5:
sheet = new BottomSheet.Builder(this).title("To " + adapter.getItem(position)).sheet(R.menu.longlist).listener(new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ListAcitivty.this.onClick(adapter.getItem(position), which);
}
}).limit(R.integer.bs_initial_list_row).build();
sheet = new BottomSheet.Builder(this).title("To " + adapter.getItem(position))
.sheet(R.menu.longlist)
.listener(new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ListAcitivty.this.onClick(adapter.getItem(position), which);
}
})
.limit(R.integer.bs_initial_list_row)
.build();
break;
case 6:
sheet = getShareActions("Hello " + adapter.getItem(position)).title("Share To " + adapter.getItem(position)).limit(R.integer.no_limit).build();
sheet = getShareActions("Hello " + adapter.getItem(position)).title("Share To " + adapter.getItem(
position)).limit(R.integer.no_limit).build();
break;
case 7:
sheet = getShareActions("Hello " + adapter.getItem(position)).title("Share To " + adapter.getItem(position)).build();
sheet = getShareActions("Hello " + adapter.getItem(position)).title("Share To " + adapter.getItem(
position)).build();
break;
case 8:
sheet = new BottomSheet.Builder(this).icon(getRoundedBitmap(R.drawable.icon)).title("To " + adapter.getItem(position)).sheet(R.menu.list).listener(new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ListAcitivty.this.onClick(adapter.getItem(position), which);
}
}).build();
sheet = new BottomSheet.Builder(this).icon(getRoundedBitmap(R.drawable.icon))
.title("To " + adapter.getItem(position))
.sheet(R.menu.list)
.listener(new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ListAcitivty.this.onClick(adapter.getItem(position), which);
}
})
.build();
final Menu menu = sheet.getMenu();
menu.getItem(0).setTitle("MenuClickListener");
menu.getItem(0).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
q.alert("OnMenuItemClickListener", "You can set OnMenuItemClickListener for each item");
q.alert(
"OnMenuItemClickListener",
"You can set OnMenuItemClickListener for each item");
return true;
}
});
menu.getItem(1).setVisible(false);
menu.getItem(2).setEnabled(false);
menu.add(Menu.NONE, 23, Menu.NONE, "Fresh meal!");
menu.findItem(23).setIcon(R.drawable.perm_group_user_dictionary);
menu.findItem(23).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
q.toast("Hello");
return true;
}
});
menu.setGroupVisible(android.R.id.empty,false);
menu.findItem(23)
.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
q.toast("Hello");
return true;
}
});
menu.setGroupVisible(android.R.id.empty, false);
break;
case 9:
sheet = new BottomSheet.Builder(this, R.style.BottomSheet_CustomizedDialog).grid().title("To " + adapter.getItem(position)).sheet(R.menu.list).listener(new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ListAcitivty.this.onClick(adapter.getItem(position), which);
}
}).build();
sheet = new BottomSheet.Builder(this, R.style.BottomSheet_CustomizedDialog).grid()
.title("To " + adapter.getItem(position))
.sheet(R.menu.list)
.listener(new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ListAcitivty.this.onClick(adapter.getItem(position), which);
}
})
.build();
sheet.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
Expand All @@ -188,7 +246,6 @@ public void onDismiss(DialogInterface dialog) {
}
});
break;

}
return sheet;
}
Expand Down
27 changes: 21 additions & 6 deletions example/src/main/res/menu/list.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/share" android:icon="@drawable/perm_group_messages" android:title="@string/share"/>
<item android:id="@+id/upload" android:icon="@drawable/perm_group_system_clock" android:title="@string/upload"/>
<item android:id="@+id/call" android:icon="@drawable/perm_group_phone_calls" android:title="@string/call"/>
<group android:id="@android:id/empty" android:visible="false">
<item android:id="@+id/help" android:icon="@drawable/perm_group_system_tools" android:title="@string/help"/>
<item
android:id="@+id/share"
android:icon="@drawable/perm_group_messages"
android:title="@string/share"
android:checked="true" />
<item
android:id="@+id/upload"
android:icon="@drawable/perm_group_system_clock"
android:title="@string/upload" />
<item
android:id="@+id/call"
android:icon="@drawable/perm_group_phone_calls"
android:title="@string/call" />
<group
android:id="@android:id/empty"
android:visible="false">
<item
android:id="@+id/help"
android:icon="@drawable/perm_group_system_tools"
android:title="@string/help" />
</group>
</menu>
</menu>
6 changes: 6 additions & 0 deletions library/src/main/java/com/cocosw/bottomsheet/BottomSheet.java
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ public View getView(int position, View convertView, ViewGroup parent) {
holder = new ViewHolder();
holder.title = convertView.findViewById(R.id.bs_list_title);
holder.image = convertView.findViewById(R.id.bs_list_image);
holder.check = convertView.findViewById(R.id.bs_list_check);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
Expand All @@ -303,12 +304,17 @@ public View getView(int position, View convertView, ViewGroup parent) {
holder.image.setEnabled(item.isEnabled());
holder.title.setEnabled(item.isEnabled());

if (holder.check != null) {
holder.check.setVisibility(item.isChecked() ? View.VISIBLE : View.GONE);
}

return convertView;
}

class ViewHolder {
private TextView title;
private ImageView image;
private ImageView check;
}
};

Expand Down
9 changes: 9 additions & 0 deletions library/src/main/res/drawable/bs_ic_check.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!-- drawable/check.xml -->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:width="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?bs_checkColor">
<path android:fillColor="#000" android:pathData="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z" />
</vector>
2 changes: 1 addition & 1 deletion library/src/main/res/layout/bs_grid_entry.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
android:layout_height="wrap_content"
style="@style/BottomSheet.GridItemTitle"
android:id="@+id/bs_list_title" />
</LinearLayout>
</LinearLayout>
27 changes: 21 additions & 6 deletions library/src/main/res/layout/bs_list_entry.xml
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
style="@style/BottomSheet.ListItem"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content"
tools:ignore="UseCompoundDrawables">

<ImageView
android:id="@id/bs_list_image"
style="@style/BottomSheet.ListItemImage"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
tools:ignore="ContentDescription" />

<ImageView
android:id="@+id/bs_list_check"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
style="@style/BottomSheet.GridItemCheck"
android:src="@drawable/bs_ic_check"
tools:ignore="ContentDescription" />

<TextView
android:id="@id/bs_list_title"
style="@style/BottomSheet.ListItemTitle" />
</LinearLayout>
android:layout_centerVertical="true"
android:layout_toRightOf="@id/bs_list_image"
android:layout_toLeftOf="@id/bs_list_check"
android:layout_alignWithParentIfMissing="true"
style="@style/BottomSheet.ListItemTitle"
tools:text="Title"/>


</RelativeLayout>
Loading