我使用listview很长时间,但这次我面临一个奇怪的问题。这就是我想把setonitemclick listner放在它上面,但它无法正常工作我无法弄明白问题在哪里。我有另一个用于listview的xml来显示数据。
的xml:
<ListView android:id="@+id/lv"
android:dividerHeight="5dp"
android:choiceMode="singleChoice"
android:clickable="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
代码:
package algonation.com.noteup;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
private NavigationDrawerFragment mNavigationDrawerFragment;
private CharSequence mTitle;
private DBManager dbManager;
private ListView listView;
private SimpleCursorAdapter adapter;
final String[] from = new String[] {DatabaseHelper.TAG_TITLE,
DatabaseHelper.TAG_TIME,DatabaseHelper._ID,
DatabaseHelper.ADD_TAG
};
final int[] to = new int[] {R.id.title, R.id.time,R.id.textView4,R.id.fullnote};
private View f;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dbManager = new DBManager(this);
dbManager.open();
final Animation animAlpha = AnimationUtils.loadAnimation(this, R.anim.alfa);
final Button btnmain =(Button)findViewById(R.id.button4);
final Button btn_addnote = (Button)findViewById(R.id.button2);
final Button btn_TakePic =(Button)findViewById(R.id.button5);
btn_addnote.setVisibility(View.INVISIBLE);
btn_TakePic.setVisibility(View.INVISIBLE);
btnmain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.startAnimation(animAlpha);
Intent i = new Intent(MainActivity.this,Camera.class);
startActivity(i);
btn_addnote.setVisibility(View.VISIBLE);
btn_TakePic.setVisibility(View.VISIBLE);
btnmain.setText("X");
}
});
Cursor cursor = dbManager.fetch();
listView = (ListView) findViewById(R.id.lv);
adapter = new SimpleCursorAdapter(MainActivity.this, R.layout.activity_view_record, cursor, from, to, 0);
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "fdfdfd", Toast.LENGTH_SHORT).show();
}
});
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}
@Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
.commit();
}
public void onSectionAttached(int number) {
switch (number) {
case 1:
mTitle = getString(R.string.title_section1);
break;
case 2:
mTitle = getString(R.string.title_section2);
break;
case 3:
mTitle = getString(R.string.title_section3);
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
getMenuInflater().inflate(R.menu.main, menu);
}
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.add:
Intent add_tag = new Intent(MainActivity.this,AddNote.class);
startActivity(add_tag);
return true;
}
return super.onOptionsItemSelected(item);
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainActivity) activity).onSectionAttached(
getArguments().getInt(ARG_SECTION_NUMBER));
}
}
}
答案 0 :(得分:1)
最后我通过做R&amp; D得到了解决方案。解决方案只是在列表元素布局的根目录中添加一行。
android:descendantFocusability="blocksDescendants"
在我的情况下,这个工作得非常好。
bytheway感谢大家。
答案 1 :(得分:0)
listView.bringToFront();
在
之前尝试这个listView.setOnItemClickListener();