Android ListView中的ImageButton会导致问题

Android ListView中的ImageButton会导致问题,android,listview,imagebutton,Android,Listview,Imagebutton,我对Android开发相对较新,遇到了以下问题: 我有一个带有相应ArrayAdapter的ListView,它提供简单的项目视图。项目视图以简单的RelatveLayout方式构造,如果使用OnItemClickListener和OnItemLongClickListener的回调方法单击或长按,则会显示一些行为。到目前为止,这一切都很好。但是,如果我使用相应的onClick回调将ImageButton添加到项目视图中,那么项目视图本身上的原始侦听器方法将不再工作。也无法再选择ListView

我对Android开发相对较新,遇到了以下问题: 我有一个带有相应ArrayAdapter的ListView,它提供简单的项目视图。项目视图以简单的RelatveLayout方式构造,如果使用OnItemClickListener和OnItemLongClickListener的回调方法单击或长按,则会显示一些行为。到目前为止,这一切都很好。但是,如果我使用相应的onClick回调将ImageButton添加到项目视图中,那么项目视图本身上的原始侦听器方法将不再工作。也无法再选择ListView中的项目。为什么?

public class ProfileActivity extends Activity implements ActionBar.TabListener {

    private static final String DEBUG_TAG = ProfileActivity.class
            .getSimpleName();

    private XMLBinder profilesDao;
    private Config config;

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the sections. We use a {@link FragmentPagerAdapter}
     * derivative, which will keep every loaded fragment in memory. If this
     * becomes too memory intensive, it may be best to switch to a
     * {@link android.support.v13.app.FragmentStatePagerAdapter}.
     */
    SectionsPagerAdapter mSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);

        getActionBar().setSubtitle("Game Profiles");

        // load profiles:
        profilesDao = new XMLBinder(this);
        try {
            config = profilesDao.deserialize(Config.class,
                    R.raw.default_profiles);
        } catch (Exception e) {
            if (BuildConfig.DEBUG) {
                Log.e(DEBUG_TAG, e.toString());
            }
        }

        // Set up the action bar.
        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager(),
                config.getCategories());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        // When swiping between different sections, select the corresponding
        // tab. We can also use ActionBar.Tab#select() to do this if we have
        // a reference to the Tab.
        mViewPager
                .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {
                        actionBar.setSelectedNavigationItem(position);
                    }
                });

        // For each of the sections in the app, add a tab to the action bar.
        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
            // Create a tab with text corresponding to the page title defined by
            // the adapter. Also specify this Activity object, which implements
            // the TabListener interface, as the callback (listener) for when
            // this tab is selected.
            actionBar.addTab(actionBar.newTab()
                    .setText(mSectionsPagerAdapter.getPageTitle(i))
                    .setTabListener(this));
        }

        // SharedPreferences pref = getSharedPreferences("AGT",
        // Context.MODE_PRIVATE);
        // SharedPreferences.Editor prefEditor = pref.edit();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.profile, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab,
            FragmentTransaction fragmentTransaction) {
        // When the given tab is selected, switch to the corresponding page in
        // the ViewPager.
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab,
            FragmentTransaction fragmentTransaction) {
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab,
            FragmentTransaction fragmentTransaction) {
    }

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        private List<Category> categories;

        /** */
        public SectionsPagerAdapter(final FragmentManager fm,
                final List<Category> categories) {

            super(fm);
            this.categories = categories;
        }

        @Override
        public Fragment getItem(int position) {

            // getItem is called to instantiate the fragment for the given page.
            // Return a PlaceholderFragment (defined as a static inner class
            // below).

            final List<Profile> profiles = config.getCategories().get(position)
                    .getProfiles();
            return PlaceholderFragment.newInstance(position + 1, profiles,
                    ProfileActivity.this);
        }

        @Override
        public int getCount() {

            // Show x total pages.
            return this.categories.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {

            return categories.get(position).getName();
        }
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment implements
            OnItemClickListener {

        private List<Profile> list;

        private ProfilesAdapter profilesAdapter;

        private Context context;

        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";

        /**
         * Returns a new instance of this fragment for the given section number.
         * 
         * @param list
         */
        public static PlaceholderFragment newInstance(int sectionNumber,
                List<Profile> list, Context context) {

            final PlaceholderFragment fragment = new PlaceholderFragment(list,
                    context);
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        public PlaceholderFragment() {
            super();
        }

        /** */
        public PlaceholderFragment(List<Profile> list, Context context) {

            this();
            this.list = list;
            this.context = context;
            this.profilesAdapter = new ProfilesAdapter(this.context,
                    R.layout.view_profile, this.list);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

            final View rootView = inflater.inflate(R.layout.fragment_profile,
                    container, false);

            // Configure the ListView with Adapter
            final ListView profilesView = (ListView) rootView
                    .findViewById(R.id.profilesView);
            profilesView.setAdapter(profilesAdapter);
            // @TODO:
            // profilesView.setDivider();
            // profilesView.setEmptyView(emptyView);
            profilesView.setOnItemClickListener(this);
            profilesAdapter.notifyDataSetChanged();

            return rootView;
        }

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {

            if (view != null) {
                final GameProfile selectedProfile = (GameProfile) parent
                        .getItemAtPosition(position);
                final Intent intent = new Intent(this.context,
                        ChessClockActivity.class);
                intent.putExtra(GameProfile.INTENT_EXTRA_SELECTED_PROFILE,
                        selectedProfile);

                startActivity(intent);
            }
        }
    }

    /**
     * @author Andy
     *
     */
    public static class ProfilesAdapter extends ArrayAdapter<Profile> {

        private Context context;
        private List<Profile> profiles;

        /** */
        public ProfilesAdapter(Context context, int resource,
                List<Profile> profiles) {

            super(context, resource, profiles);

            this.context = context;
            this.profiles = profiles;
        }

        @Override
        public View getView(final int position, final View convertView,
                final ViewGroup parent) {

            View profileView = convertView;

            if (null == profileView) {
                final LayoutInflater inflater = (LayoutInflater) this.context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                profileView = inflater.inflate(R.layout.view_profile, null);
            }

            final TextView name = (TextView) profileView
                    .findViewById(R.id.profile_name);
            name.setText(profiles.get(position).getTitle());
            final TextView hint = (TextView) profileView
                    .findViewById(R.id.profile_desc);
            hint.setText(profiles.get(position).getHint());
            hint.setMaxLines(1);
            hint.setEllipsize(TruncateAt.END);
            return profileView;
        }
    }
}

我不知道这是否有用

ImageView yourImg=(ImageView).findViewById(R.id.icon);
yourImg.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
    // things you want to do 
  }
});
使用


希望它能有所帮助

原因可能有数千个,但没有一个与您的问题相关。你应该包括你的代码,以便我们可以知道你做错了什么。可能重复的Thx链接:就是这样。如果我将android:genderantfocusability=blocksDescendants添加到RelativeLayout中,它会起作用。我没有尝试你的建议,但是我认为,它也不起作用,因为这是一个焦点问题:在项目视图的布局中添加以下属性可以解决这个问题:android:genderantfocusability=blocksDescendants就是这样,thx!项目的包含布局上的第一个属性已经足够了,但是。@Andy您好,第二个属性不是必需的。我很高兴这有帮助:
android:descendantFocusability="blocksDescendants"
android:focusable="false"