Scrolling child view in gallery - android

Each item in my gallery is a custom view. One of the child's view is a gridView. When I'm scrolling the gallery everything works fine, but it wont scroll by touching the gridView. Its difficult to explain, I hope someone will understand me!
Touching and scrolling any part of the custom view suppose to trigger the scrolling? Or maybe only the imageView will trigger the scrolling?
public View getView(int position, View convertView, ViewGroup parent) {
final SubProduct subProduct=subProducts.get(position);
int quantity=subProduct.getQuantity();
int size=subProduct.getSizes().get(0).getWidth();
String productName=subProduct.getProductName();
int productPrice=subProduct.getSizes().get(0).getPrice();
int columnWidth = 0;
View view=convertView;
if (view==null){
holder=new SubProductHolder();
//The main container
holder.myLinearLayout= new LinearLayout(this.myContext);
holder.myLinearLayout.setOrientation(1);//vertical
//The custome button
LayoutInflater inflater = (LayoutInflater)
myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v=(View)inflater.inflate(R.layout.custom_sub_product_layout, null);
LinearLayout btnLayout = (LinearLayout)v.findViewById(R.id.customSunProductButtonLayout);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(PRODUCT_BOTTUN_WIDTH,PRODUCT_BOTTUN_HEIGHT);
btnLayout.setLayoutParams(llp);
tvSubProductDescription=(TextView)v.findViewById(R.id.tvSubProductDescription);
tvSubProductPrice=(TextView) v.findViewById(R.id.tvSubProductPrice);
holder.btnProduct=btnLayout;
holder.btnProduct.setGravity(Gravity.CENTER);
holder.TvLinearLayout= new LinearLayout(this.myContext);
holder.TvLinearLayout.addView(holder.btnProduct);
//frame
holder.image = new ImageView(this.myContext);
holder.image.setBackgroundColor(Color.WHITE);
LinearLayout.LayoutParams llp2 = new LinearLayout.LayoutParams(FRAME_WIDTH,FRAME_WIDTH);
holder.image.setLayoutParams(llp2);
//grid
holder.grid=new GridView(myContext);
//the number of rows is the round number of quantity sqaure
int numberOfColomns=(int) Math.floor((int) Math.sqrt(quantity));
holder.grid.setNumColumns(numberOfColomns);
holder.grid.setLayoutParams(new RelativeLayout.LayoutParams(PRODUCT_PICTURE_WIDTH,PRODUCT_PICTURE_WIDTH));
}else{
holder=(SubProductHolder)view.getTag();
}
//set text in description tv
tvSubProductDescription.setText(quantity+" "+productName+" "+size+"X"+size);
tvSubProductPrice.setText("$"+productPrice);
//set the grid
holder.grid.setAdapter(new emptySquaresAdapter(quantity,myContext,columnWidth,columnHeight));
holder.relativeGridLayout=new RelativeLayout(myContext);
holder.relativeGridLayout.addView(holder.image);
holder.relativeGridLayout.addView(holder.grid);
//set button and picture to layout
holder.myLinearLayout.addView(holder.relativeGridLayout);
holder.myLinearLayout.addView(holder.TvLinearLayout);
return holder.myLinearLayout;
}
static class SubProductHolder
{
ImageView image;
GridView grid;
RelativeLayout relativeGridLayout;
LinearLayout btnProduct;
LinearLayout TvLinearLayout;
LinearLayout myLinearLayout;
}

You have to use TouchListeners for galleryView since on clicking on its child view will not trigger scroll for galleryView

You have to use TouchListeners. Also, you're better off going with a ViewPager and using fragments. To my knowledge, Gallery is deprecated.

Related

Android GridView Scrolling issue

I'm facing a Problem, that my GridView "clips"/"glitches" after I scroll to the 4th row or longer than 2 seconds.
I got 58 Items, which get loaded via a Adapter into the GridView. An Item consists of a filename and an Image of the Item (Thumbnail). Each Thumbnail has a width and height of 100dp and is loaded into a ImageButton via the Framework "Glide" without resizing, crop or anything else. Simple Glide.load(ressource).into(imageButton).
Please see the attached images to follow my further explanation.
After Scrolling I would expect, that my Items are Aligned like the first 15-19 Items before. Unfortunately it is scrolling only the "last Item" of the 4th row from the GridView. That mean's that at Point 2 (red digit within the Picture) all the other items appearing for a short period if I scroll through them.
After scrolling further the whole GridView and Scrollbar get's "destroyed" and only a small amount of Item's appear and lastly 1 or none item's appear. I can see, that the Scrollbar is decreasing very fast, after scrolling.
GridView xml Properties (within main_activity.xml):
android:columnWidth="100dp"
android:gravity="center"
android:horizontalSpacing="5dp"
android:numColumns="auto_fit"
android:verticalSpacing="20dp"
android:visibility="visible"
GridViewAdapter Code:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
_layoutInflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
_view = new View(_context);
_view = _layoutInflater.inflate(R.layout.single_item, null);
TextView textView = _view.findViewById(R.id.textView);
final Item item = _items.get(position);
textView.setText(item.getName());
ImageButton imageButton = _view.findViewById(R.id.imageButton);
Glide.with(_context).load(item.getDrawableRessource()).into(imageButton);
imageButton.setOnClickListener(click -> {
_iOnItemClickListener.onClick(item);
});
}
return _view;
}
Thanks for any helpful advice.
Try this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
_view = convertView;
if (convertView == null) {
_layoutInflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
_view = _layoutInflater.inflate(R.layout.single_item, null);
}
TextView textView = _view.findViewById(R.id.textView);
final Item item = _items.get(position);
textView.setText(item.getName());
ImageButton imageButton = _view.findViewById(R.id.imageButton);
Glide.with(_context).load(item.getDrawableRessource()).into(imageButton);
imageButton.setOnClickListener(click -> {
_iOnItemClickListener.onClick(item);
});
return _view;
}

how to wrap the width of gridview item dynamically in android

I am developing an application in android.i want to set the gridview item dynamically as shown in the below image.someone suggest with a solution .
You can use RecyclerView with StaggeredGridLayoutManager instead of GridView. You can find a good tutorial here StaggeredGridLayoutManager
You can control the size of your item from within the grid adapter - more specifically in your overridden getView() method :
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v = LayoutInflater.from(ctx).inflate(R.layout.grid_item, null);
// ...
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(AbsListView.LayoutParams.
WRAP_CONTENT, AbsListView.LayoutParams.WRAP_CONTENT);
lp.width = getResources().getDimensionPixelSize(R.dimen.card_width);
lp.height = getResources().getDimensionPixelSize(R.dimen.card_height);
v.setLayoutParams(lp);
return v;
}

android rootview malfunctionning

I'm creating a fragment that contains two listviews ,the two in a linearlayout that has as orientation:horizontal;
the second listview is in a horizontalscrollview;
the problem is in the second listview;
I should have in each row of the listview 6 textview there's one 100 textview in the first row. The other rows have the correct number of textviews.
When I was debugging, I deduced that the problem is in my adapter in the getview function, I'm not sure what the exact issue is.
Here is the adapter of the listview that I'm referring to:
#Override
public View getView(int position,View convertView,ViewGroup parent)
{
View row = convertView;
if(row==null)
{
row =inflater.inflate(R.layout.list_live_2,parent,false);
}
List<String> stages = getItem(position);
LinearLayout linear_layout= (LinearLayout) row.findViewById(R.id.layout_stages);
LinearLayout.LayoutParams layoutparams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 71);
layoutparams.setMargins(0,0,10,0);
for(int j=0;j<stages.size();++j)
{
TextView textView =new TextView (row.getContext());
textView.setText(stages.get(j));
textView.setLayoutParams(layoutparams);
linear_layout.addView(textView);
}
return row;
}

Android - add Layout views into ListView

the problems is, that I wan't to make ListView with elements which are containing image, description and two buttons. I'm making them in my own BaseAdapter extension, but fragment which is containing ListView is closing (wihtout errors in logcat..). I've found, that ListView is working well, when I'm not returning layout-type elements. So there is my sample with 'sample linear layout', which is not working.. Is there any possibility, to show layouts in ListView?
Here is my code:
Creating part:
lv = (ListView) getView().findViewById(R.id.main_wall_ambajes_lv);
AmbajAdapter aa = new AmbajAdapter(getActivity().getApplicationContext(), StaticData.ambajes);
lv.setAdapter(aa);
My getView method from adapter:
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout ll = new LinearLayout(getActivity());
ll.setOrientation(LinearLayout.VERTICAL);
ImageView iv = new ImageView(getActivity());
iv.setImageBitmap(placeholderBitmap);
ll.addView(iv);
ll.addView(iv);
ll.addView(iv);
ll.addView(iv);
return ll;
}
I don't know why you don't have any error however I don't think you proceed the correct way.
Usually you create the layout in the xml file of the layout folder and only inflate it in the getView(), for example as follow :
private LayoutInflater mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
#Override
public View getView(int position, View view, ViewGroup parent) {
if (view == null) {
view = mInflater.inflate(R.layout.your_custom_layout, parent, false);
}
//your code for setting the image or other things goes here
//for example if you have a textView
TextView textView = (TextView) view.findViewById(R.id.my_textview_id);
textView.setText("my custom text for this cell");
return (view);
}
and your_custom_layout is simply the xml file of your layout.
Note that for performance reason due to cell recycling I only inflate the view when it is null and I only read once the LayoutInflater context and put it in mInflater. However for the best performance you should use a ViewHolder, but it is out of the scope of your question.

When I change first item components,Last item is effected

I have a custom row that contains a text view and 3 Clickable Image Views.
onClick on any one of the ImageView this Image changes to another one.
My problem is that when I click on Image1 on row1, the image changed in both row1 and row9 as well, and when I click on row2, the image changed in row2 and row10 as well.. so On. I don't know why.
But I think it it about scrolling.
This is getView() in my Adapter:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (vi == null) {
vi = inflater.inflate(R.layout.comprow, null);
}
TextView text = (TextView) vi.findViewById(R.id.name);
text.setText(data.get(position));
return vi;
}
And this is OnClick function for the first ImageView
public void one(View v) {
RelativeLayout row = (RelativeLayout)v.getParent();
ImageView im1 = (ImageView) row.findViewById(R.id.one);
ImageView im2 = (ImageView) row.findViewById(R.id.two);
ImageView im3 = (ImageView)row.findViewById(R.id.three);
im1.setImageResource(R.drawable.c0);
im2.setImageResource(R.drawable.b1);
im3.setImageResource(R.drawable.b2);
simpleAdpt.notifyDataSetChanged();
}
When you don't use of id, your changed repeat in all row because you worked with position,then you must set id to each row that create in getView(), now if you have a few choice, you can handle with array of integer with size of list, that in default is equal 0 then if you press one row the value of row changed, then in show image check the value of this row, if is zero then let in default, else check the value and select image that you want to show, I hope that you understand my word

Categories

Resources