I have to create a view like this -
There will be three items shown with a show more option
On Click of show more the list will grow to 10 items which will be scrollable .
What could be the optimal way to implement this kind of view ?
Use HorizontalScrollView
On the first population of the scrollview detect the click on the 4th position and on the 4th item click reload the data and call adapter.notifyDataSetChanged()
fro this purpose you can Add 4 element at first time.and detect which Item is clicked by user if this is on 3rd position then you can add more element using this code snippets
HorizontalScrollView scrollView = (HorizontalScrollView) findViewById(R.id.scrollView1);
LinearLayout topLinearLayout = new LinearLayout(this);
// topLinearLayout.setLayoutParams(android.widget.LinearLayout.LayoutParams.FILL_PARENT,android.widget.LinearLayout.LayoutParams.FILL_PARENT);
topLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
for (int i = 0; i < 15; i++){
final ImageView imageView = new ImageView (this);
imageView.setTag(i);
imageView.setImageResource(R.drawable.ic_launcher);
topLinearLayout.addView(imageView);
imageView.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
Log.e("Tag",""+imageView.getTag());
}
});
}
scrollView.addView(topLinearLayout);
hope so it will work for you.
enjoy your coding time:)
Use RecyclerView instead.
set the LinearLayoutManager's orientation HORIZONTAL, like this:
recyclerView.setLayoutManager(new LinearLayoutManager(context, HORIZONTAL, false);
Good luck.
Related
I have a LinearLayout with adding some images dynamically with their click listeners.
When I'm clicking on an ImageView I want to remove Onclick listener from all the ImageViews to prevent clicking on any ImageView again.
Can anyone suggest any permanent idea without boolean variable.
Set the onClickListener to null:
imageView.setOnClickListener(null);
You could give your LinearLayout an id, and then loop over all children and set the listener to null;
LinearLayout layout = findViewById(R.id.imagesLayout);
View v = null;
for(int i=0; i<layout.getChildCount(); i++) {
if(v instanceOf ImageView) //you dont have to do this when there are only imageViews
v.setOnClickListener(null)
}
You can do this like:
imageView.setOnClickListener(null);
or
imageView.setClickable(false);
I have the following scenario:
In a layout, I have imageviews aligned horizontally and vertically as shown in image.
First, which layout should be used for this purpose? RelativeLayout or FrameLayout? ListView inside the layout?
Also, instead of writing setOnClickListener for every imageview, how can I write just one click listener to get the clicked imageview?
A GridView is perfect for this. For more information on GridView, look here.
As for your onClickListener question: there is no easy way to do this, but what you can do is something like this:
Have an array containing every ImageView id like so:
public static final int[] IMAGE_VIEWS = {R.id.imageView1, R.id.imageView2, R.id.imageView3 /*etc*/}; //Make sure to list from the first imageview to the last image view in correct order
Define an onClickListener
private View.OnClickListener imageViewListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i < IMAGE_VIEWS.length; i++) {
if (v.getId() == IMAGE_VIEWS[i]) {
doSomethingWithImageViewClick(i); //if ImageView 4 was clicked, variable 'i' will be 3 (because arrays start at index = 0).
}
}
}
}
Set the onClickListeners for all your imageViews:
final ImageView view1 = (ImageView) view.findViewById(R.id.imageView1);
view1.setOnClickListener(imageViewListener);
//etc
Use 'GridView' in the layout and use 'setOnItemClickListener' to handle click events,
You can find more details in below link
GridView.
GridView can achieve the effect. I think you can look at this Grid View.
Here's my question :
I have a listview (with adapter) which contains multiple inflated layout for each row and I want to smooth scroll programmatically to a specific layout.
Exemple :
this is one listview row:
------------------
- -
- layout1 -
- -
- layout2 -
- layout3 -
- etc... -
------------------
I know that in a Scrollview we can use getY() of the layout view but when i did this for my specific layout in listview it return me 0. But I can't use scrollview because I need to get SwipeRefreshLayout (Or we can get it in Scrollview ?)
I hope I was clear enough, if this is not the case, please tell me.
If you are triggering scroll by action like onClick.
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View row = listView.getChildAt(0);
View layout = row.findViewById(R.id.layout_to_scroll_to);
listView.smoothScrollToPositionFromTop(rowPositionToScrollTo, layout.getTop());
}
});
If you are triggering scroll on page load. Add this in onCreate or onActivityCreated for Activity or Fragment, accordingly.
getListView().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
listView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
View row = listView.getChildAt(0);
View layout = row.findViewById(R.id.layout_to_scroll_to);
listView.smoothScrollToPositionFromTop(rowPositionToScrollTo, layout.getTop());
}
});
Change smoothScrollToPositionFromTop to setSelectionFromTop for no scroll animation.
Hope this helps !
I am using a ViewPager with Dynamic Views as shown in this link. I am adding each page to the viewpager. The code to create each page and add it to the pager adapter is as follows
public View createPage( )
{
TableLayout table = new TableLayout(mycontext);
TableRow row = new TableRow(mycontext);
for(int i=0; i <= num_of_views; i++)
{
TableLayout catTable = new TableLayout(mycontext);
TableLayout.LayoutParams params = new TableLayout.LayoutParams();
params.height = LayoutParams.MATCH_PARENT;
TableRow catName = new TableRow(mycontext);
catName.setGravity(0x03);
catName.setLayoutParams(params);
TextView cat = new TextView(mycontext);
TableRow.LayoutParams RowParams=
new TableRow.LayoutParams(pixeltoDp(0),pixeltoDp(85));
RowParams.weight = 1;
cat.setLayoutParams(RowParams);
cat.setPadding(35, 0, 5, 0);
cat.setGravity(0x13);
cat.setSingleLine(true);
cat.setHorizontallyScrolling(true);
cat.setFocusable(true);
cat.setFocusableInTouchMode(true);
cat.setLines(1);
cat.setText(data.getCats().get(i).getName());
cat.setTextColor(color);
cat.setSelected(true);
catName.addView(cat);
catTable.addView(catName);
for(int j=0; j < 5; j++)
{
TableRow trChild = new TableRow(mycontext);
trChild.setLayoutParams(params);
trChild.setGravity(0x11);
for (int k=0; k<4; k++)
{
TextView app = new TextView(mycontext);
TableRow.LayoutParams tableRowParams=
new TableRow.LayoutParams(pixeltoDp(0),pixeltoDp(80));
margin = pixeltoDp(11);
tableRowParams.weight = 1;
app.setLayoutParams(tableRowParams);
app.setGravity(0x11);
app.setSingleLine(true);
app.setTextSize(pixeltoDp(12));
app.setTextColor(Color.BLACK);
app.setTypeface(verdana);
int pos = j+(k*5);
if((pos < mysize))
{
Drawable icon = getMyDrawable();
app.setCompoundDrawablesWithIntrinsicBounds(null, icon, null, null);
app.setText("some Name");
app.setTag(i+":"+pos);
app.setEllipsize(TextUtils.TruncateAt.END);
app.setOnClickListener(onMyClick);
}
trChild.addView(app);
}
catTable.addView(trChild);
}
TableRow.LayoutParams tableRowParams=
new TableRow.LayoutParams
(TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.WRAP_CONTENT);
margin = pixeltoDp(5);
tableRowParams.setMargins(margin,0,margin, pixeltoDp(18));
catTable.setLayoutParams(tableRowParams);
}
row.setGravity(0x03);
table.addView(row);
return table;
}
The OnClickListener for the textview in the page is as follows
private View.OnClickListener onAppClick=new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String gettext = (String)v.getTag();
String[] splittext = gettext.split(":");
Log.d("TAG","Click Event is Triggered");
}
};
The problem I am facing is that the viewpager slides when I swipe on the screen but if my finger starts the swipe from the text with the onclicklistener the page does not slide, also the click event is not called. If I click on the text view the click event is called. I am not able to figure why the viewpager in not flipping the pages if the swipe starts from the text view with click event.
To find out the reason for the bug I added a ontouchlistener to the viewpager and found that the touch event of the viewpager is called when the swipe happens on the part of the page where there is no text view and the touch event is not called at all if the swipe starts on top of the text view. If I remove the onclicklistener of the textview then the entire viewpager flips properly without any issue. So the problem is that the click event of the textview is stopping the touch events of the viewpager. Please tell me if I am doing something wrong in my create page code. The view which I create in this function is added to the viewpager through the pageadapter as shown in the tutorial I mentioned in the beginning.
Finally I found that there is a bug in the latest version of ViewPager where a TextView with a click event, singleLine set as true and gravity set as center is not passing the touch events to the ViewPager if the finger is moved starting on the top of the textview. If we make the singleLine as false or remove the Center gravity then the events are passed to the ViewPager then it can fling. We can't use both the center gravity and setsingleline(true) for the textview or a button with sinlgeline set as true. I have reported this as a bug here
See the 'apply single line' source here: https://github.com/android/platform_frameworks_base/blob/7bfb1e7f46d2ed3df6cc467bccec95b5999d9cc4/core/java/android/widget/TextView.java#L7877
It seems that there is somewhat of a solution: you can use setMaxLines(1) instead of setSingleLine()and it will work without impairing the system. However I can confirm your previous answer in which you say it's a bug
I have added a bunch of images to a ViewFlipper and now I am performing an onClick event in the flipper. For this I would like to know the current child position so that I can perform some operations in an array. Is there anyway to find out the position of the current child.
Use this to get the current Child position:
flipper.getDisplayedChild();
And this to set child number to view:
flipper.setDisplayedChild(8);
In addflipperimages(ViewFlipper flipper) method you are adding bunch of images to ViewFlipper for that you are creating imageview, set tag to imageview, set imageview clickable true then write onclick method to imageview. go through the fallowing code it may works for you
here ids[] is an array of image ids
private void addFlipperImages(ViewFlipper flipper) {
int imageCount = ids.length;
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
for (int count = 0; count <imageCount; count++) {
ImageView imageView = new ImageView(this);
Bitmap imbm = BitmapFactory.decodeResource(this.getResources(),ids[count]);
imageView.setImageBitmap(imbm);
imageView.setLayoutParams(params);
imageView.setTag(count);
imageView.setClickable(true);
imageView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
int id=(Integer) v.getTag();
Toast.makeText(ImageSliderVertical.this, id+"", Toast.LENGTH_LONG).show();
}
});
flipper.addView(imageView);
flipper.setTag(count);
}
}
Make use of indexOfChild().
Check this Post
How can I programmatically display a ViewFlipper's second child?
May this works for you.
I used this flipper.indexOfChild(flipper.getCurrentView())