Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ParallaxGridView onItemClickListener position #3

Open
omidaminiva opened this issue Apr 13, 2015 · 7 comments
Open

ParallaxGridView onItemClickListener position #3

omidaminiva opened this issue Apr 13, 2015 · 7 comments

Comments

@omidaminiva
Copy link

when I click on any item position is total number of items. for example with 21 items in grid when I click in any item, position is 21.
parallaxGridView.setOnItemClickListener(new setOnItemClickListener(){
@OverRide
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Log.v("Item Clicked",position+"");
}
});

@kanytu
Copy link
Owner

kanytu commented Apr 14, 2015

parallaxGridView.setOnItemClickListener(new setOnItemClickListener()

This doesn't seem like a good implementation of AdapterView.OnItemClickListener(). There is no such thing as new setOnItemClickListener()

Here is an example, that's tested and it's working, of an implementation on the example project:

} else if (view.getId() == R.id.btnGrid) {
    mContainer.removeAllViews();
    final View v = getLayoutInflater().inflate(R.layout.include_gridview, mContainer, true);
    ParallaxGridView mGridView = (ParallaxGridView) v.findViewById(R.id.view);
    mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Log.v("MainActivity", position + "");
        }
    });
    mGridView.setParallaxView(getLayoutInflater().inflate(R.layout.view_header, mGridView, false));
    mGridView.setAdapter(mAdapter);
    mGridView.setNumColumns(2);
}

@omidaminiva
Copy link
Author

that was a typo
parallaxGridView.setOnItemClickListener(new OnItemClickListener(){
@OverRide
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Log.v("Item Clicked",position+"");
}
});

you are calling setParallaxView after setOnItemClickListener would that make any difference?

@omidaminiva
Copy link
Author

this is how my code looks like:

public class LiteProfileDetailFragment extends Fragment implements, OnItemClickListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

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

        ViewGroup root = (ViewGroup) inflater.inflate(
                R.layout.lite_profile_detail, container, false);
        getLoaderManager().initLoader(1, null, this);
        initUi(root);

        return root;
    }

    private void initUi(View view) {

        gv = (ParallaxGridView) view.findViewById(R.id.parallaxGridView);

        ll = new LinearLayout(getActivity());
        ll.setOrientation(LinearLayout.HORIZONTAL);
        bg = new ImageView(getActivity());
        ll.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT));
        name = new TextView(getActivity());
        name.setLines(2);
        ll.addView(bg);
        ll.addView(name);
        ll.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                pop = popupWindowForHeader(false);
                pop.showAsDropDown(v, 0, 0);

            }
        });
        gv.setParallaxView(ll);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int loaderId, Bundle args) {
        // some sql query
                return (loader);

    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
                       build(data) ;
            lpdAdapter.swapCursor(data);
    }

    private void build(Cursor cursor) {
        // some code
            name.setText(room.name + System.getProperty("line.separator")
                    + room.description);
            lpdAdapter = new LiteProfileDetailAdapter(getActivity(), room);
            gv.setAdapter(lpdAdapter);
            gv.setOnItemClickListener(this);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        loader = null;

    }

    private void restart(int id) {
        if (this.isVisible())
            getLoaderManager().restartLoader(id, null, this);
    }

    public class LiteProfileDetailAdapter extends CursorAdapter {

        private Profile room;

        public LiteProfileDetailAdapter(Context context, Profile room) {
            super(context, null, true);
            this.room = room;
        }

        @Override
        public void bindView(View convertView, Context context,
                final Cursor cursor) {
            ViewHolder vh = new ViewHolder();
            vh.color = (LinearLayout) convertView.findViewById(R.id.color);
            vh.icon = (ImageView) convertView.findViewById(R.id.icon);
            vh.name = (TextView) convertView.findViewById(R.id.name);

            vh.color.setBackgroundColor(Color.parseColor(room.color));
            imageLoader.displayImage(cursor.getString(cursor
                    .getColumnIndexOrThrow(DataConstants.ICON_URL)), vh.icon);
            vh.name.setText(cursor.getString(cursor
                    .getColumnIndexOrThrow(DataConstants.NAME)));
            convertView.setTag(cursor.getString(cursor
                    .getColumnIndexOrThrow(DataConstants.WUID)));
        }

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            View convertView = LayoutInflater.from(context).inflate(
                    R.layout.lite_row_widget_items, (ViewGroup) null);

            return convertView;
        }

        class ViewHolder {
            public LinearLayout color;
            public ImageView icon;
            public TextView name;

        }
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        Log.v("Item Clicked",position+"");

    }
}

I simplified the code so hopefully there is no typo it again :)

@kanytu
Copy link
Owner

kanytu commented Apr 15, 2015

I don't see anything wrong with the code. There should be something wrong with anything else. I tested the method and it is in fact working. Can you check if the returned view from onItemClick is the one you clicked or it's any other?

@omidaminiva
Copy link
Author

view and parent are correct
parent comes as com.poliveira.apps.parallaxlistview.ParallaxGridView
and view as android.widget.LinearLayout which my widget item is
there are other factors that could cause the problem:
1- I have tabhhost and tab widget in my xml file with ParallaxGridView
2- I am using https://github.com/nostra13/Android-Universal-Image-Loader to load my image in ParallaxGridView's header
3- I am using cursorLoader and cursoradapter

@jahertor
Copy link

jahertor commented Jul 1, 2015

I had the same problem. Viewing the code I saw that you must to subtract the "numHeadersAndPlaceholders" cause they affect to the count of items. This is the headersCount * numberOfColumns, so:

final int numOfHeadersAndPlaceholders = mGridView.getHeaderViewCount() * getResources().getInteger(R.integer.num_columns);
mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        final Item item = items.get(position - numOfHeadersAndPlaceholders);
        /* ... */
    }
});

@omidaminiva
Copy link
Author

Then that is a bug that should be looked at.
I just used tags to identify the row. So inside bindView I had convertView.setTag(cursor.getString(cursor .getColumnIndexOrThrow(DataConstants.WUID))); which wuid for me is key to my sql table. Then in OnItemClickListener I used Log.v("Item Clicked",(String)view.getTag()); then I know which row in my sql table is clicked on. You can also use position instead of WUID. So in bindView it would be convertView.setTag(position) and in OnItemClickListener Log.v("Item Clicked",(Integer)view.getTag()+"");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants