所以,我想基于一个数组动态填充(部分)我的导航抽屉,其中每个数组项都有自己的按钮'。到目前为止,在互联网上搜索还没能帮到我,所以我可以帮助我完成这项工作。
我想要做的是,有一个默认列表,看起来有点像这样:
<item android:title="@string/navdrawer_title1">
<menu>
//Fill this part dynamically
</menu>
</item>
<item android:title="@string/navdrawer_title2">
<menu>
<item
android:id="@+id/nav_generalsettings"
android:icon="@drawable/ic_menu_gallery"
android:title="@string/navdrawer_generalsettings" />
<item
android:id="@+id/nav_stuffmanager"
android:icon="@drawable/ic_menu_gallery"
android:title="@string/navdrawer_stuffmanager" />
</menu>
</item>
所以,一个设定的部分,有一点动态填充。我想动态地填充整个抽屉并不是那么难,但是我在动态填充抽屉的一小部分时会遇到更多麻烦。任何人都可以帮我吗?
答案 0 :(得分:1)
绝对可以动态填充抽屉,我想这是一个非常常见的用例。抽屉只是一个基于屏幕的视图的容器,它可以包含任何东西。在您的情况下,对于ListView
,并且支持适配器连接到阵列,它听起来很完美。
答案 1 :(得分:0)
是的,您必须定义自定义适配器。
例如:
NavigationItem.java
public class NavigationItem {
public static interface OnItemSelectedListener {
public void selected();
}
private int image;
private String title;
private OnItemSelectedListener listener;
public NavigationItem(int image, String title, OnItemSelectedListener listener) {
this.image = image;
this.title = title;
this.listener = listener;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public OnItemSelectedListener getListener() {
return listener;
}
}
NavigationItemAdapter.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.digitalnotebook.R;
import com.digitalnotebook.navigation.NavigationItem;
import java.util.List;
public class NavigationItemAdapter extends BaseAdapter {
private Context context;
private List<NavigationItem> items;
public NavigationItemAdapter(Context context, List<NavigationItem> items) {
this.context = context;
this.items = items;
}
@Override
public int getCount() {
return this.items.size();
}
@Override
public NavigationItem getItem(int position) {
return this.items.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (convertView == null) {
// Create a new view into the list.
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.fragment_navigation_item, parent, false);
}
// Set data into the view.
ImageView ivItem = (ImageView) rowView.findViewById(R.id.item_icon);
TextView tvTitle = (TextView) rowView.findViewById(R.id.item_title);
NavigationItem item = this.items.get(position);
tvTitle.setText(item.getTitle());
ivItem.setImageResource(item.getImage());
return rowView;
}
}
fragment_navigation_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="?android:attr/activatedBackgroundIndicator">
<ImageView
android:id="@+id/item_icon"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:contentDescription="Image Icon"/>
<TextView
android:id="@+id/item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/item_icon"
android:text="Default"
android:textSize="18dp" />
</RelativeLayout>
在您的activity.java中:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
refreshNavigationBar();
}
protected void refreshNavigationBar() {
List<NavigationItem> items = new ArrayList<NavigationItem>();
try {
for (final Course course : courseManager.getActiveCourses()) {
items.add(new NavigationItem(R.drawable.ic_action_go_to_today, course.getName(), new NavigationItem.OnItemSelectedListener() {
@Override
public void selected() {
Intent activity = new Intent(getApplicationContext(), DigitalNotebookActivity.class);
activity.putExtra(Constants.COURSE_ID_PARAM, course.getId().intValue());
startActivity(activity);
}
}));
}
} catch (SQLException e) {
Log.e("HomeActivity", "Error to load active courses.", e);
}
mDrawerListView.setAdapter(new NavigationItemAdapter(this, items));
}
刷新方法,从数据库加载数据
这是一个例子,也许没有编译,我已经动态修改,但这是一个好的开始。