我有一个滑动抽屉,抽屉里有一个带有4个项目的ListView。我想使用performItemClick
以编程方式单击抽屉中的一个项目,但无论我尝试什么代码似乎什么都不做。当单击该项时,它应该用另一个片段替换当前在视图中的片段,并更改操作栏标题。但是,当我运行performItemClick
时,这些都不会发生。
以下是performItemClick
中正在接收广播的BroadcastReceiver
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
mDrawerList.performItemClick(mDrawerList.getAdapter().getView(1, null, null),1,mDrawerList.getAdapter().getItemId(1));
Log.d("receiver", "Got message + item clicked");
}
};
以下是实际活动的代码,其中包含performItemClick
和抽屉本身,以及抽屉碎片(performItemClick
靠近底部)。
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.Fragment;
import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.support.v4.view.GravityCompat;
import android.text.Html;
@SuppressLint("ResourceAsColor")
public class MainActivity extends SherlockFragmentActivity {
private String Data, Band, Number, Class, Teacher, Title, Date, Type, Description, DescriptionAll, DescriptionCheck;
// Declare Variables
DrawerLayout mDrawerLayout;
LinearLayout mDrawerLinear;
TextView mWelcomePerson;
ListView mDrawerList;
ActionBarDrawerToggle mDrawerToggle;
MenuListAdapter mMenuAdapter;
String actionbar_colors, background_colorsString;
private String Show_View;
String[] title;
String[] count;
int[] icon;
private String counterss;
private int counters;
Fragment fragment1 = new FragmentsView();
Fragment fragment2 = new FragmentsHomeworkDue();
Fragment fragment3 = new FragmentSettings();
private CharSequence mDrawerTitle;
private CharSequence mTitle;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences sharedprefers = getSharedPreferences("first_run", Context.MODE_PRIVATE );
if(sharedprefers.contains("first_run")){
}else{
SharedPreferences.Editor localEditor = getSharedPreferences("first_run", Context.MODE_PRIVATE ).edit();
localEditor.putString("first_run", "ran for the first time");
localEditor.commit();
Intent intenter = new Intent(this, MainActivity.class);
finish();
startActivity(intenter);
}
setContentView(R.layout.drawer_main);
SharedPreferences sharedpre = getSharedPreferences("show_view", Context.MODE_PRIVATE );
Show_View = sharedpre.getString("show_view", "");
SharedPreferences sharedpref = getSharedPreferences("actionbar_color", Context.MODE_PRIVATE );
if(!sharedpref.contains("actionbar_color")){
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#03a9f4")));
}else{
actionbar_colors = sharedpref.getString("actionbar_color", null);
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor(actionbar_colors)));
}
ActionBar bar = getSupportActionBar();
bar.setIcon(
new ColorDrawable(getResources().getColor(android.R.color.transparent)));
mTitle = mDrawerTitle = getTitle();
SharedPreferences Today_Homework = getApplicationContext().getSharedPreferences("due_today", Context.MODE_PRIVATE);
if(Today_Homework.contains("duetoday_content")){
parse_due_today_string();
}else{
counterss = "";
}
if(Show_View.equals("Homework Due")){
title = new String[] { "Homework Due", "Schedule", "Options", "Logout" };
icon = new int[] { R.drawable.ic_action_duehomework, R.drawable.ic_action_go_to_today, R.drawable.ic_action_settings, R.drawable.ic_action_logout };
if(counterss == null && counterss.isEmpty()){
count = new String[] { "", "", "", "" };
}
else{
count = new String[] { counterss, "", "", "" };
}
}else{
if(counterss == null && counterss.isEmpty()){
count = new String[] { "", "", "", "" };
}else{
count = new String[] { "", counterss, "", "" };
}
title = new String[] { "Schedule", "Homework Due", "Options", "Logout" };
icon = new int[] { R.drawable.ic_action_go_to_today, R.drawable.ic_action_duehomework, R.drawable.ic_action_settings, R.drawable.ic_action_logout };
}
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLinear = (LinearLayout) findViewById(R.id.left_drawer);
mWelcomePerson = (TextView) findViewById(R.id.Person);
mWelcomePerson.setText("Lincoln Bernard");
mDrawerList = (ListView) findViewById(R.id.listview_drawer);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
mMenuAdapter = new MenuListAdapter(MainActivity.this, title,
icon, count);
mDrawerList.setAdapter(mMenuAdapter);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.drawer_open,
R.string.drawer_close) {
public void onDrawerClosed(View view) {
// TODO Auto-generated method stub
super.onDrawerClosed(view);
}
public void onDrawerOpened(View drawerView) {
// TODO Auto-generated method stub
getSupportActionBar().setTitle(mDrawerTitle);
super.onDrawerOpened(drawerView);
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
selectItem(0);
}
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
new IntentFilter("up_navigation"));
}
public void parse_due_today_string() {
SharedPreferences Today_Homework = getApplicationContext().getSharedPreferences("due_today", Context.MODE_PRIVATE);
String Due_Today = Today_Homework.getString("duetoday_content", "");
ArrayList<String> description = new ArrayList<String>( );
StringBuilder DescriptionAll = new StringBuilder();
InputStream is = new ByteArrayInputStream(Due_Today.getBytes());
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
try {
int i = 0;
while ((Due_Today = reader.readLine()) != null) {
String[] part = Due_Today.split("\",\"", -1);
int noOfItems = part.length;
int counter=0;
DescriptionCheck = counter<noOfItems ? part[counter] : "";
counter++;
Band = counter<noOfItems ? part[counter] : "";
counter++;
Number = counter<noOfItems ? part[counter] : "";
counter++;
Class = counter<noOfItems ? part[counter] : "";
counter++;
Teacher = counter<noOfItems ? part[counter] : "";
counter++;
Title = counter<noOfItems ? part[counter] : "";
counter++;
Date = counter<noOfItems ? part[counter] : "";
counter++;
Type = counter<noOfItems ? part[counter] : "";
counter++;
Data = counter<noOfItems ? part[counter] : "";
counter++;
if (Type != null && !Type.isEmpty()){
counters = ++i;
counterss = Integer.toString(counters);
Log.i("Counter","Number of children: "+ counters);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
if (mDrawerLayout.isDrawerOpen(mDrawerLinear)) {
mDrawerLayout.closeDrawer(mDrawerLinear);
} else {
mDrawerLayout.openDrawer(mDrawerLinear);
}
}
return super.onOptionsItemSelected(item);
}
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) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
if(Show_View.equals("Homework Due")){
switch (position) {
case 0:
ft.replace(R.id.content_frame,fragment2);
break;
case 1:
ft.replace(R.id.content_frame, fragment1);
break;
case 2:
ft.replace(R.id.content_frame, fragment3);
break;
}
}else{
switch (position) {
case 0:
ft.replace(R.id.content_frame,fragment1);
break;
case 1:
ft.replace(R.id.content_frame, fragment2);
break;
case 2:
ft.replace(R.id.content_frame, fragment3);
break;
}
}
ft.commit();
mDrawerList.setItemChecked(position, true);
/
setTitle(title[position]);
mDrawerLayout.closeDrawer(mDrawerLinear);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getSupportActionBar().setTitle(mTitle);
}
@Override
protected void onPause() {
super.onPause();
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
}
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("receiver", "Got");
mDrawerList.performItemClick(mDrawerList.getAdapter().getView(1, null, null),1,mDrawerList.getAdapter().getItemId(1));
Log.d("receiver", "Got message");
}
};
@Override
public void onBackPressed() {
FragmentManager manager = getSupportFragmentManager();
if (manager.getBackStackEntryCount() > 0) {
manager.popBackStack();
} else {
super.onBackPressed();
}
}
}
MenuListAdapter
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;
public class MenuListAdapter extends BaseAdapter {
Context context;
String[] mTitle;
String[] mCount;
int[] mIcon;
LayoutInflater inflater;
public MenuListAdapter(Context context, String[] title,
int[] icon, String[] count) {
this.context = context;
this.mTitle = title;
this.mIcon = icon;
this.mCount = count;
}
@Override
public int getCount() {
return mTitle.length;
}
@Override
public Object getItem(int position) {
return mTitle[position];
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
TextView txtTitle;
ImageView imgIcon;
TextView txtCount;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.drawer_list_item, parent,
false);
txtTitle = (TextView) itemView.findViewById(R.id.title);
txtCount = (TextView) itemView.findViewById(R.id.counter);
imgIcon = (ImageView) itemView.findViewById(R.id.icon);
txtTitle.setText(mTitle[position]);
txtCount.setText(mCount[position]);
imgIcon.setImageResource(mIcon[position]);
return itemView;
}
}
答案 0 :(得分:1)
我在Drawer的Expandable listview适配器中做了类似的事情。
View.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
switch (groupPosition)
{
case 0:
switch (childPosition)
{
case 0:
if (groupPosition == 0 && childPosition == 0)
{ some code }
编辑:此开关在每个相应的项目上单击启动不同的活动(或更改视图,如果片段)..在您的情况下,它们将是片段..