I'trying to display two lists inside my ScrollView. I found following solution: Using a ListAdapter to fill a LinearLayout inside a ScrollView layout (filling LinearLayout with custom adapter).
This solution works fine but I don't know how to handle click event on list items. It would be simple if I used ListView (onItemClick method with position parameter) but LinearLayout doesn't support the same functionality.
My layout:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:scrollbars="none"
android:background="@color/background_gray_light">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
// here should be view with infobox, not implemented yet //
<LinearLayout
android:id="@+id/story_list_inbox"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/archive"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:background="@color/actionbar_bg"
android:text="@string/archive"
android:textSize="16dp"
android:textColor="@color/text_gray"
android:gravity="center_horizontal" />
<LinearLayout
android:id="@+id/story_list_archive"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Populating the lists:
public class StoryListFragment extends Fragment {
public StoryListFragment() {}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_story_list, container,
false);
TextView archiveText = (TextView) view.findViewById(R.id.archive);
archiveText.setText(archiveText.getText().toString().toUpperCase());
final List<Story> storyList = new ArrayList<Story>();
// filling the list ....
StoryListAdapter adapter = new StoryListAdapter(getActivity(), R.layout.story_list_item, storyList);
LinearLayout inbox = (LinearLayout) view.findViewById(R.id.story_list_inbox);
final int inboxCount = adapter.getCount();
for (int i = 0; i < inboxCount; i++) {
View item = adapter.getView(i, null, null);
inbox.addView(item);
}
/* archive */
// filling the list ....
LinearLayout archive = (LinearLayout) view.findViewById(R.id.story_list_archive);
final int archiveCount = adapter.getCount();
for (int i = 0; i < archiveCount; i++) {
View item = adapter.getView(i, null, null);
archive.addView(item);
}
return view;
}
}
Question:
How should I make the LinearLayout list items clickable to be able to get the index of the clicked item?
What I even want to achieve:
Maybe I'm not even using the right solution, what I'm trying to achieve here is to create something like a message inbox, which consists of two lists - one for newest messages and the second one for the archived messages. I also want to display some content on the top of the lists, so I put it all inside a ScrollView. It should look like this:
SOLUTION:
for (int i = 0; i < inboxCount; i++) {
final int index = i;
View item = adapter.getView(i, null, null);
item.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
System.out.println("Clicked " + index);
startActivity(new Intent(v.getContext(), StoryIntroActivity.class));
}
});
inbox.addView(item);
}