Reputation: 215
I'm trying to make a horizontal scroll view inside a ListView. I have the ListView working, but putting data inside the horizontalscrollview(HSV) is not working out for me. Please advise! (I coded the listview, tested and now trying to add a horizontalscrollview)
The HSV is going to be for every listitem.
So basically for my logic on how I am approaching this: I have my listview adapter, i decided to place the HSV inside the adapter so it loops through each listItem and places a HSV inside that.
My Xml lookis like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:baselineAligned="false"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip" android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/txtProjectName" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/txtProjectDescription" />
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/projectTasks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/txtProjectTasks" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</LinearLayout>
Then I created a custom adapter to run through all the projects.
public class ProjectListAdapter extends ArrayAdapter<Projects> {
int resource;
String response;
Context context;
ArrayList<Tasks> taskArray = null;
// Initialize adapter
public ProjectListAdapter(Context context, int resource,
List<Projects> items) {
super(context, resource, items);
this.resource = resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout projectView;
// Get the current project object
Projects project = getItem(position);
//
// Inflate the view
if (convertView == null) {
projectView = new LinearLayout(getContext());
String inflater = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater vi;
vi = (LayoutInflater) getContext().getSystemService(inflater);
vi.inflate(resource, projectView, true);
} else {
projectView = (LinearLayout) convertView;
}
TextView PROJECT_NAME = (TextView) projectView
.findViewById(R.id.txtProjectName);
TextView PROJECT_DESCRIPTION = (TextView) projectView
.findViewById(R.id.txtProjectDescription);
PROJECT_NAME.setText(project.getNAME());
PROJECT_DESCRIPTION.setText(project.getDESCRIPTION());
taskArray = new ArrayList<Tasks>();
taskArray = (ArrayList<Tasks>) project.getTasks();
for (Tasks tasks : taskArray) {
HorizontalScrollView TASKS = (HorizontalScrollView) projectView
.findViewById(R.id.projectTasks);
LinearLayout taskLayout = (LinearLayout) projectView
.findViewById(R.id.projectTasks);
TextView taskTxt = (TextView) projectView
.findViewById(R.id.txtProjectTasks);
taskTxt.setText(tasks.getTASK_ID());
taskLayout.addView(taskTxt);
}
return projectView;
}
}
That last bit of code is where the adapter will loop through to make the HSV, but something isn't working how I would like, please please help!
Upvotes: 4
Views: 5332
Reputation: 4860
In your code this line is not ok, you're creating a new linearLayout but it's not from your custom view:
projectView = new LinearLayout(getContext());
So try this:
View projectView = convertView;
// Inflate the view
if (projectView == null) {
String inflater = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater vi;
vi = (LayoutInflater) getContext().getSystemService(inflater);
projectView = vi.inflate(resource, false);
}
And define your projectView as a View, not a LinearLayout.
Upvotes: 0
Reputation: 7087
Please follow following link to have HSV in ListView: http://www.dev-smart.com/archives/34
OR
@Override
public boolean onTouchEvent(MotionEvent event) {
boolean handled = mGesture.onTouchEvent(event);
return handled;
}
Then, add the following code which will decide to steal the event from the item children and give it to our onTouchEvent, or let it be handled by them.
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch( ev.getActionMasked() ){
case MotionEvent.ACTION_DOWN:
mInitialX = ev.getX();
mInitialY = ev.getY();
return false;
case MotionEvent.ACTION_MOVE:
float deltaX = Math.abs(ev.getX() - mInitialX);
float deltaY = Math.abs(ev.getY() - mInitialY);
return ( deltaX > 5 || deltaY > 5 );
default:
return super.onInterceptTouchEvent(ev);
}
}
Finally, don't forget to declare the variables in your class:
private float mInitialX;
private float mInitialY;
Source: Horizontal ListView in Android?
Upvotes: 1