我正在尝试创建导航抽屉,在点击时更改碎片。它与ArrayAdapter一起工作正常,但由于我创建了自己的带有web tut的CustomAdapter,它只显示日志并且不会更改片段并隐藏navDrawer。这是我的适配器:
package com.gymcourses.qiteq.gymcourses;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomAdapter extends BaseAdapter {
String[] result;
Context context;
int[] imageId;
private static LayoutInflater inflater = null;
private String mode;
public CustomAdapter(Activity mainActivity, String[] prgmNameList, int[] prgmImages, String mode) {
// TODO Auto-generated constructor stub
result = prgmNameList;
this.mode = mode;
context = mainActivity;
imageId = prgmImages;
inflater = (LayoutInflater) context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return result.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
/*
@Override
public boolean areAllItemsEnabled(){
return true;
}
*/
@Override
public boolean isEnabled(int arg0){
return true;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public class Holder {
TextView tv;
ImageView img;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder = new Holder();
View rowView;
// mode "favorites" or "menu"
switch (mode) {
case "favorites":
rowView = inflater.inflate(R.layout.simple_listview_layout, null);
break;
case "menu":
rowView = inflater.inflate(R.layout.navigation_drawer_custom_layout, null);
break;
default:
rowView = inflater.inflate(R.layout.simple_listview_layout, null);
Log.v("CustomAdapter.java: ", "doenst works");
}
holder.tv = (TextView) rowView.findViewById(R.id.textView1);
holder.img = (ImageView) rowView.findViewById(R.id.imageView1);
holder.tv.setText(result[position]);
holder.img.setImageResource(imageId[position]);
rowView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String positionS = Integer.toString(position);
Log.v("Item selected: ", positionS);
}
});
return rowView;
}
}
我的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#FFFFFF"
android:clickable="true">
<ImageView
android:id="@+id/imageView1"
android:layout_gravity="center"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="@drawable/ic_menu"
android:padding="5dp"/>
<TextView
android:id="@+id/textView1"
android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textSize="15dp"
android:text="TextView"
android:padding="0dp"/>
</LinearLayout>
主要活动: 类:
package com.gymcourses.qiteq.gymcourses;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.zip.Inflater;
public class menu extends AppCompatActivity {
private String[] mPlanetTitles;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
public void editcontacts(View view) {
Log.v("editcontacts", "clicked");
Intent intent = new Intent(menu.this, EditContact.class);
startActivity(intent);
}
private class DrawerItemClickListener implements ListView.OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
selectItem(position);
}
}
private void selectItem(int position){
Fragment fragment = new CategoryFragment();
Bundle args = new Bundle();
args.putInt(CategoryFragment.ARG_CATEGORY_NUMBER, position);
fragment.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.drawerContent, fragment).commit();
mDrawerList.setItemChecked(position, true);
setTitle(mPlanetTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
mPlanetTitles = getResources().getStringArray(R.array.drawerLayoutList);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mDrawerList = (ListView) findViewById(R.id.drawer);
int[] mImages = {R.drawable.ic_profile, R.drawable.ic_favorites, R.drawable.ic_notifications, R.drawable.ic_messages, R.drawable.ic_news, R.drawable.ic_discoveries, R.drawable.ic_artists, R.drawable.ic_users, R.drawable.ic_popular, R.drawable.ic_recommended, R.drawable.ic_genres, R.drawable.ic_settings, R.drawable.ic_settings};
//mDrawerList.setAdapter(new CustomAdapter(this, mPlanetTitles, mImages, "menu"));
mDrawerList.setAdapter(new ArrayAdapter<String>(getApplicationContext(), R.layout.simple_listview_layout, mPlanetTitles));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
//Toolbar toolbar = new Toolbar(getApplicationContext());
mDrawerToggle = new android.support.v7.app.ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close){
public void onDrawerClosed(View view){
super.onDrawerClosed(view);
getSupportActionBar().setTitle(mTitle);
//invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView){
super.onDrawerOpened(drawerView);
getSupportActionBar().setTitle(mDrawerTitle);
//invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
//getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu);
Log.v("Item selection", "start");
selectItem(0);
Log.v("Item selection", "false");
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu){
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
if (mDrawerToggle.onOptionsItemSelected(item)){
return true;
}
/*
switch(item.getItemId()){
case R.id.action_websearch:
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY, getSupportActionBar().getTitle());
if (intent.resolveActivity(getPackageManager()) != null){
startActivity(intent);
} else {
Toast.makeText(this, "Sorry, there's no web browser avilable", Toast.LENGTH_SHORT).show();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
*/
return super.onOptionsItemSelected(item);
}
public static class CategoryFragment extends Fragment {
public static final String ARG_CATEGORY_NUMBER = "planet_number";
public CategoryFragment() {
// Empty constructor required for fragment subclasses
}
//FRAGMENT BUILDER//
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.menu_fragment, container, false);
/*int i = getArguments().getInt(ARG_CATEGORY_NUMBER);
String planet = getResources().getStringArray(R.array.drawerLayoutList)[i];
int imageId = getResources().getIdentifier(planet.toLowerCase(Locale.getDefault()), "drawable", getActivity().getPackageName());
//((ImageView) rootView.findViewById(R.id.image)).setImageResource(imageId);
ImageView imageView = (ImageView) rootView.findViewById(R.id.imageFragment);
//getActivity().setTitle("Fragment");
*/
String message = Integer.toString(getArguments().getInt(ARG_CATEGORY_NUMBER));
Log.v("Position: ", message);
switch (getArguments().getInt(ARG_CATEGORY_NUMBER)){
case 0:
rootView = inflater.inflate(R.layout.profile_layout, container, false);
final String phoneNumber = "666555444";
final String emailContant = "example@example.com";
final String skypeContact = "skajpaj";
String nickname = "Qiteq";
String description = "My profile description";
TextView nicknamefield = (TextView) rootView.findViewById(R.id.nickname);
nicknamefield.setText(nickname);
TextView descriptionfield = (TextView) rootView.findViewById(R.id.description);
descriptionfield.setText(description);
ImageView avatarfield = (ImageView) rootView.findViewById(R.id.avatar);
Drawable newAvatar = getResources().getDrawable(R.drawable.cat);
avatarfield.setImageDrawable(newAvatar);
if (phoneNumber!=null){
TextView contact1field = (TextView) rootView.findViewById(R.id.contact1);
contact1field.setText(phoneNumber);
contact1field.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel:" +phoneNumber));
startActivity(callIntent);
}
});
}
if (emailContant!=null){
TextView contact2field = (TextView) rootView.findViewById(R.id.contact2);
contact2field.setText(emailContant);
contact2field.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent sendEmail = new Intent(Intent.ACTION_SEND);
sendEmail.setType("text/plain");
sendEmail.putExtra(Intent.EXTRA_EMAIL, new String[] {emailContant});
/*
sendEmail.putExtra(Intent.EXTRA_SUBJECT, "Subject");
sendEmail.putExtra(Intent.EXTRA_TEXT, "I'm email body.");
*/
startActivity(Intent.createChooser(sendEmail, "Send Email"));
}
});
}
break;
case 1:
rootView = inflater.inflate(R.layout.favorites_layout, container, false);
String[] values = new String[]{"asdasd", "asdasdas", "asdasd", "asdasd"};
int[] images = {R.drawable.ic_menu, R.drawable.ic_menu, R.drawable.ic_menu, R.drawable.ic_menu};
ListView lv = (ListView) rootView.findViewById(R.id.favoritesListView);
lv.setAdapter(new CustomAdapter(getActivity(), values, images, "menu"));
break;
case 2:
break;
case 3:
rootView = inflater.inflate(R.layout.news_layout, container, false);
break;
}
return rootView;
}
}
@Override
public void setTitle(CharSequence title){
mTitle = title;
getSupportActionBar().setTitle(mTitle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState){
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
和布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.gymcourses.qiteq.gymcourses.menu">
<FrameLayout
android:id="@+id/drawerContent"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ListView
android:id="@+id/drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:layout_gravity="start"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
有整个项目:
https://drive.google.com/file/d/0B_yo7L-EKsdpWFVUSG91Z2dZbUE/view?usp=sharing
答案 0 :(得分:1)
这是有问题的一行:
rowView.setOnClickListener...
无法调用DrawerItemClickListener.onItemClick,因为rowView.setOnClickListener
拦截了点击
同时删除android:clickable =&#34; true&#34;来自&#34;我的布局&#34;