gpt4 book ai didi

java - 手动在抽屉导航中添加图标

转载 作者:太空宇宙 更新时间:2023-11-04 12:40:46 26 4
gpt4 key购买 nike

我很困惑在抽屉导航上的标题旁边添加图标,有人知道该怎么做吗?我按照这个喜欢创建一些抽屉导航here ,

这是我的代码

FragmentDrawer.java

public class FragmentDrawer extends Fragment {

private static String TAG = FragmentDrawer.class.getSimpleName();

private RecyclerView recyclerView;
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
private NavigationDrawerAdapter adapter;
private View containerView;
private static String[] titles = null;
private FragmentDrawerListener drawerListener;

public FragmentDrawer() {

}

public void setDrawerListener(FragmentDrawerListener listener) {
this.drawerListener = listener;
}

public static List<NavDrawerItem> getData() {
List<NavDrawerItem> data = new ArrayList<>();



// preparing navigation drawer items
for (int i = 0; i < titles.length; i++) {
NavDrawerItem navItem = new NavDrawerItem();
navItem.setTitle(titles[i]);
data.add(navItem);
}
return data;

}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// drawer labels
titles = getActivity().getResources().getStringArray(R.array.nav_drawer_labels);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflating view layout
View layout = inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
recyclerView = (RecyclerView) layout.findViewById(R.id.drawerList);

adapter = new NavigationDrawerAdapter(getActivity(), getData());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity(), recyclerView, new ClickListener() {
@Override
public void onClick(View view, int position) {
drawerListener.onDrawerItemSelected(view, position);
mDrawerLayout.closeDrawer(containerView);
}

@Override
public void onLongClick(View view, int position) {

}
}));

return layout;
}


public void setUp(int fragmentId, DrawerLayout drawerLayout, final Toolbar toolbar) {
containerView = getActivity().findViewById(fragmentId);
mDrawerLayout = drawerLayout;
mDrawerToggle = new ActionBarDrawerToggle(getActivity(), drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getActivity().invalidateOptionsMenu();
}

@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
getActivity().invalidateOptionsMenu();
}

@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
toolbar.setAlpha(1 - slideOffset / 2);
}
};

mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerLayout.post(new Runnable() {
@Override
public void run() {
mDrawerToggle.syncState();
}
});

}

public static interface ClickListener {
public void onClick(View view, int position);

public void onLongClick(View view, int position);
}

static class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {

private GestureDetector gestureDetector;
private ClickListener clickListener;

public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final ClickListener clickListener) {
this.clickListener = clickListener;
gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}

@Override
public void onLongPress(MotionEvent e) {
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null) {
clickListener.onLongClick(child, recyclerView.getChildPosition(child));
}
}
});
}

@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {

View child = rv.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
clickListener.onClick(child, rv.getChildPosition(child));
}
return false;
}

@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}

@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

}


}

public interface FragmentDrawerListener {
public void onDrawerItemSelected(View view, int position);
}
}

字符串.xml

<string name="nav_item_home">Home</string>
<string name="nav_item_friends">Friends</string>
<string name="nav_item_notifications">Messages</string>
<string name="nav_item_calculator">BMI Calculator</string>
<string name="nav_item_listview">Test Json</string>
<string name="nav_item_testabs">Test Tabs</string>


<!-- navigation drawer item labels -->
<string-array name="nav_drawer_labels">
<item>@string/nav_item_home</item>
<item>@string/nav_item_friends</item>
<item>@string/nav_item_notifications</item>
<item>@string/nav_item_calculator</item>
<item>@string/nav_item_listview</item>
<item>@string/nav_item_testabs</item>
</string-array>

<array name="nav_drawer_icons">
<item>@drawable/arm1</item>
<item>@drawable/abs2</item>
<item>@drawable/back</item>
<item>@drawable/leg</item>
<item>@drawable/shoulder</item>
<item>@drawable/achest</item>
</array>

MainActivity.java

private void displayView(int position) {
Fragment fragment = null;
String title = getString(R.string.app_name);
switch (position) {
case 0:
fragment = new HomeFragment();
title = getString(R.string.nav_item_home);
break;
case 1:
fragment = new FriendsFragment();
title = getString(R.string.nav_item_friends);
break;
case 2:
fragment = new MessagesFragment();
title = getString(R.string.nav_item_notifications);
break;
case 3:
fragment = new CalculatorFragment();
title = getString(R.string.nav_item_calculator);
break;
case 4:
fragment = new MovieFragment();
title = getString(R.string.nav_item_listview);
break;
case 5:
fragment = new TabFragment();
title = getString(R.string.nav_item_testabs);
default:
break;
}

if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.commit();

// set the toolbar title
getSupportActionBar().setTitle(title);
}
}

NavDrawwerItem.java

public class NavDrawerItem {
private boolean showNotify;
private String title;
private int icon;

public NavDrawerItem() {

}

public NavDrawerItem(boolean showNotify, String title, int icon) {
this.showNotify = showNotify;
this.title = title;
this.icon = icon;
}

public boolean isShowNotify() {
return showNotify;
}

public void setShowNotify(boolean showNotify) {
this.showNotify = showNotify;
}

public String getTitle() {
return title;
}

public int getIcon(){
return this.icon;
}

public void setTitle(String title) {
this.title = title;
}

public void setIcon(int icon){
this.icon = icon;
}

最佳答案

您需要在抽屉列表 xml 中添加 Imageview,并自定义适配器并设置图标,

<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<ImageView
android:id="@+id/drawer_item_icon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="10sp"
android:layout_marginRight="10sp"
android:src="@drawable/ic_launcher" />

<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:paddingTop="8sp"
android:paddingBottom="8sp"
android:paddingLeft="15sp" />

</LinearLayout>

关于java - 手动在抽屉导航中添加图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36854085/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com