-3

I'm relatively new to the Android development and encountered the following problem: I have a ListView with a corresponding ArrayAdapter which delivers simple item views. The item views are structured in a simple RelatveLayout manner revealing some behavior if clicked or long-pressed using the callback methods of OnItemClickListener and OnItemLongClickListener. This works fine so far. However, if I add an ImageButton to the item view with a corresponding onClick-callback, the original listener-methods on the item view itself don't work any more. The items in the ListView can't be selected any more as well. Why?

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;
        }
    }
}
Andy
  • 488
  • 4
  • 13
  • There can be thousands of reason and none of which could be related to your problem. You should include your codes with it so that we can know what did you do wrong. –  Jan 19 '15 at 08:55
  • possible duplicate of [android: cant click on listview row with imagebutton](http://stackoverflow.com/questions/11428303/android-cant-click-on-listview-row-with-imagebutton) – jaibatrik Jan 19 '15 at 09:00
  • Thx for the link: that's it. If I add android:descendantFocusability="blocksDescendants" to the RelativeLayout it works. – Andy Jan 19 '15 at 09:17

2 Answers2

0

Dont know if it's gonna help

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

@Override
public void onClick(View v) {
    // things you want to do 
  }
});
  • didn't try your suggestion, but think, doesn't work as well, because it's a problem of focusability: Adding the following attribute to the Layout of the item view solves this:android:descendantFocusability="blocksDescendants" – Andy Jan 19 '15 at 09:20
0

use

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

hope it helps

Syed Raza Mehdi
  • 4,067
  • 1
  • 31
  • 47