Android ListView中的ImageButton会导致问题

时间:2015-01-19 08:51:45

标签: android listview imagebutton

我对Android开发相对较新,遇到了以下问题: 我有一个带有相应ArrayAdapter的ListView,它提供简单的项目视图。项目视图以简单的RelatveLayout方式构建,如果使用OnItemClickListener和OnItemLongClickListener的回调方法单击或长按,则会显示某些行为。这到目前为止工作正常。但是,如果我使用相应的onClick-callback将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;
        }
    }
}

2 个答案:

答案 0 :(得分:0)

不知道它是否会有所帮助

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

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

答案 1 :(得分:0)

使用

android:descendantFocusability="blocksDescendants"
android:focusable="false"

希望有所帮助