Open Listview "Subitems" - android

I would like to populate my listview with subitems.
My target is, when someone clicks at a listview row, this listview shows a subitem, just like this:
Image
I shouldn't call this a subitem, but I found that this was the best way to describe what I'm trying to do.
I'm not using libraries to do that, I just included in my listview rows an layout, and I'm trying to hide and show it when someone clicks on it.
My Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout"
android:layout_width="fill_parent"
android:layout_height="400sp"
android:orientation="horizontal"
android:padding="2sp"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="200sp"
android:id="#+id/relativeLayout3">
</RelativeLayout>
<include layout="#layout/activity_subitem"
android:layout_width="wrap_content"
android:id="#+id/subitem"
android:layout_height="200sp"
android:layout_below="#+id/relativeLayout3"
android:layout_alignParentStart="true" />
</RelativeLayout>
I built a simple adapter, and I'm trying to implement there my ideas, but is not working, because nothing happens
public View getView(final int position, final View convertView,
final ViewGroup parent) {
View v = super.getView(position, convertView, parent);
layout = (RelativeLayout) v.findViewById(R.id.layout);
subitem = v.findViewById(R.id.subitem);
if (flag){
subitem.setVisibility(View.GONE);
layout.getLayoutParams().height = 200;
}
v.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
subitem.setVisibility(View.VISIBLE);
layout.getLayoutParams().height = 400;
flag = false;
}
});
return v;
}
Can you guys help me?
Thank you.

Related

Adding ImageView Inside Base Adapter Programatically

I have a GridView adapter(For gallery used).
What i want is to put Image(Check Image) when the photo is clicked.
But I am unable to do that with this code.
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
cbClicked = false;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.imagelayout, parent, false);
final CheckBox cb = (CheckBox) view.findViewById(R.id.name);
final ImageView img = (ImageView) view.findViewById(R.id.img);
final LinearLayout imgTop = (LinearLayout) view.findViewById(R.id.imgTop);
ImageView img2 = new ImageView(ActivityReviewUpload.this);
img2.setImageResource(R.drawable.ic_my_loc);
imgTop.addView(text);
return view;
}
And for my XML is this.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/imgTop"
android:layout_width="match_parent"
android:layout_height="120dp">
<ImageView
android:id="#+id/img"
android:layout_width="match_parent"
android:layout_height="120dip"
android:background="#color/colorBlack"
android:padding="2dp" />
</LinearLayout>
<CheckBox
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
This is an example of what I'm doing.
In this example it's number,
what I'm doing is i will put just CHECK in the selected images.
The image is not showing.
what am i doing wrong in this part?
thanks
There's some problem with your BaseAdapter:
You should use ViewHolder or ReyclerView with GridLayoutManager to avoid laggy when scrolling your ListView.
Look at this block of code:
ImageView img2 = new ImageView(ActivityReviewUpload.this);
img2.setImageResource(R.drawable.ic_my_loc);
imgTop.addView(text);
Above code will be called everytime you sroll your ListView. Therefore, you always add new ImageView to each row when scrolling. You should remove it.
How to achieve what you want? Do it as below:
// declare selectedPosition = -1 in your `Adapter`
// in your get view added below code:
img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
selectedPosition = position;
notifyDataSetChanged();
}
});
cb.setChecked(selectedPosition == position? true: false);
Those code will change checkbox state every time you click to your image.

ImageView over ImageView in a GridView

i am trying to use an ImageView over an ImageView in a GridView. I already found this quesiton Android, ImageView over ImageView and it is really familiar with my problem, but i want to use this Layout descripted in the question inside a GridView.
So i created a .xml-file containing the <GridView></GridView> (i think this is not really useful to show, so i skip this file) and a .xml-file with the layout i want to use in every cell in the gridview. This is the following file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/rl_act_question_list_alternative" >
<ImageView
android:id="#+id/iv_question_list_country"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/germany" />
<ImageView
android:id="#+id/iv_question_list_solved"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="#drawable/icon_question_solved" />
As the next step i am creating a subclass of a BaseAdapter overriding the method getView(int position, View convertView, ViewGroup parent) with the following code:
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(ActivityLevelsAlternative.this).inflate(R.layout.act_question_list_alternative_item, null);
}
ImageView mCountry = (ImageView) convertView.findViewById(R.id.iv_question_list_country);
ImageView mIconSolved = (ImageView) convertView.findViewById(R.id.iv_question_list_solved);
final VOQuestion question = mLevel.getAllQuestions().get(position);
mCountry.setImageResource(question.getPicture());
mIconSolved.setImageResource(mPrefManager.isQuestionSolved(question.getId()) ? R.drawable.icon_question_solved : R.drawable.icon_question_unsolved);
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(getApplicationContext(), ActivityQuestion.class);
i.putExtra(ActivityQuestion.QUESTION_ID, question.getId());
ActivityLevelsAlternative.this.startActivity(i);
}
});
return convertView;
}
But the the result is not the expected... see: http://q63i.img-up.net/Screenshotc656.png
My question: how can i get the check mark over the country image?
Try to change your check mark image like this:
<ImageView
android:id="#+id/iv_question_list_solved"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignTop="#id/iv_question_list_country"
android:layout_alignLeft="#id/iv_question_list_country"
android:layout_alignRight="#id/iv_question_list_country"
android:layout_alignBottom="#id/iv_question_list_country"/>

Delete button on ListView items (like for iPhone)

I have a ListView (with an Adapter of my own) where I'd like to add a delete button by swaping an item on the ListView (like on iPhones).
I really don't know how to do it and where to start ...
Could you please give me some hints ?
Thanks
What you want to implement is a custom ListView. You need a layout for your row, here's an example
res/layout/row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/transparent">
<TextView android:id="#+id/Browse_DateTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent" />
<ImageButton android:id="#+id/delete"
android:layout_width="wrap_content"
android:layout_height="fill_parent" />
</LinearLayout>
Then you need to overide the getView() method of your adapter, a little like this:
setListAdapter(new ArrayAdapter<Object>(this, R.layout.row, R.id.Browse_DateTime, ourRows) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)BrowseActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
TextView time = (TextView) v.findViewById (R.id.Browse_DateTime);
time.setText(ourRows[position].dateTime);
ImageButton delete = (ImageButton) v.findViewById(R.id.delete);
delete.setFocusable(false);
delete.setImageDrawable(BrowseActivity.this.getResources().getDrawable(R.drawable.deletebutton));
delete.setOnClickListener(BrowseActivity.this);
delete.setId(position);
return v;
}
};)
Best regards.
PS: this is cut&paste from my code, BrowseActivity is just the name of the activity this code resides in, R.layout.row is my row.xml file, you name it any which way, just put it in /res/layout/, and if your delete button is an imagebutton, you DO need the delete.setFocusable(false); (try it without and see why).

How do I make this listview unselectable?

I want to make a listview unselectable and unclickable. I'm talking about the color change that occurs when I click on a list item. The code is given below. I'm a newbie in android so please be kind.
from : listitem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="8px">
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"/>
<TextView
android:id="#+id/data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16px"/>
</LinearLayout>
from : details.java
TestActionAdapter() {
super(TestDetails.this, R.layout.action_list_item, actions);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TestAction action = actions.get(position);
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.action_list_item, parent, false);
TextView label = (TextView) view.findViewById(R.id.label);
label.setText(action.getLabel());
TextView data = (TextView) view.findViewById(R.id.data);
data.setText(action.getData());
return view;
}
Check out the answer here:
How to disable list items...
Basically extend ArrayAdapter, and add these 2 functions:
#Override
public boolean areAllItemsEnabled() {
return false;
}
#Override
public boolean isEnabled(int position) {
return false;
}
If all you want is to prevent all rows highlighting on click just use ListView android:listSelector="#android:color/transparent"
When you return view from getView(..) Method, just put in view.setEnabled(false) before return view .

getview in listadapter

In my listadapter code, I would like to indent my view. To do that i add some left padding but that doesnt seem to work.
EDIT: main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout
android:layout_width="80dip" android:layout_height="fill_parent"
android:gravity="right|center_vertical">
<ImageView android:id="#+id/item_image" android:layout_width="wrap_content"
android:src="#drawable/icon" android:layout_height="wrap_content"></ImageView>
</LinearLayout>
<FrameLayout android:id="#+id/frame"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_weight="1" >
</FrameLayout>
</LinearLayout>
any suggestions why padding would not work ?? I want to move the image x dp to the right.
#Override
public final View getView(final int position, final View convertView, final ViewGroup parent) {
if (convertView == null) {
final LinearLayout layout = (LinearLayout) layoutInflater.inflate( R.layout.main, null);
layout.setPadding(100, 8,8,8);
return layout;
} else {
//do stuff
}
}
thanks
try this code
#Override
public final View getView(final int position, final View convertView, final ViewGroup parent) {
final View layout = View.inflate(context, R.layout.main, null);
layout.setPadding(100, 8,8,8);
return layout;
}
You are taking the reference of a xml file ..... you should get reference to the layout defined in xml file or any other view of xml file to set params ......
You can set param on the view not on xml file....
If setting padding does not work, you could always try:
layout.offsetLeftAndRight(100);
I guess .. you can give padding in your xml layout using android:padding...... (left or right or top or bottom) . Specifying them in xml layout file will indent the ImageView as per requirements.

Categories

Resources