이미지와 다양한 정보가 포함 된 이벤트 목록을 수평 LinearLayout에 표시하고 있습니다. 사용자가 LinearLayout에 포함 된 정보를 클릭하면 새 Activity를 시작하려고합니다.안드로이드 : 동적 인 ScrollView에서 LinearLayout 클릭 가능
목록 (EventListActivity)를 표시하는 활동이 .xml 파일이 있습니다
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/crimson"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<LinearLayout
android:id="@+id/event"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:clickable="true"
android:onClick="listItemClick"
android:background="?android:attr/selectableItemBackground" >
<ImageView
android:id="@+id/logo"
android:layout_width="50px"
android:layout_height="50px"
android:layout_marginLeft="5px"
android:layout_marginRight="20px"
android:layout_marginTop="5px"
android:clickable="true"
android:onClick="listItemClick"
android:src="@drawable/alabama" >
</ImageView>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="4.85"
android:text="@+id/title"
android:textColor="@color/white"
android:textSize="20px" >
</TextView>
<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="4.85"
android:text="@+id/date"
android:textColor="@color/white"
android:textSize="10px" >
</TextView>
<TextView
android:id="@+id/venue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="4.85"
android:text="@+id/venue"
android:textColor="@color/white"
android:textSize="10px" >
</TextView>
</LinearLayout>
</LinearLayout>
</ScrollView>
목록은 다음 관련 단원과 함께 EventListActivity.java 파일에 채워집니다
public class EventListActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Event[] eventList = null;
try {
eventList = getEventList();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
MobileArrayAdapter adapter = new MobileArrayAdapter(this, eventList);
setListAdapter(adapter);
}
public void listItemClick(View view)
{
// launch new Activity
}
public static Event[] getEventList() throws Exception
{
String url = "http://url.com";
String json = new JSONReader().execute(url).get();
JSONArray jsonarr = new JSONArray(json);
// eventually from web service
Event[] eventList = new Event[jsonarr.length()];
for (int i = 0; i < jsonarr.length(); i++)
{
JSONObject jsonobj = jsonarr.getJSONObject(i);
String title = jsonobj.getString("eventName");
String date = "April " + (i+1);
String time = jsonobj.getString("eventTime");
String sport = jsonobj.getString("eventType");
String opponent = jsonobj.getString("opponent");
String venue = jsonobj.getString("eventVenue");
String location = jsonobj.getString("location");
// Location l = new Location(location);
eventList[i] = new Event(title, date, time, sport, opponent, venue, location);
}
return eventList;
}
어떻게 할 수 사용자가 LinearLayout의 어떤 부분을 클릭하고 새로운 Activity를 시작할 수 있도록 허용합니까? 원래, OnItemClickListener를 사용하여이 작업을 시도했지만 ListView를 사용하지 않는다는 것을 깨달았습니다. 그래서 나는 이런 식으로 할 것이라고 생각했지만, 주된 문제는 이벤트가 동적으로 채워지고 onClick
이 하나의 매개 변수 인 View
만을 사용하기 때문에 클릭 한 이벤트를 지정하는 방법을 모르겠습니다. 어떻게해야합니까?
왜 내가 "스크롤 뷰"를 넣었는지 알 수 있습니까? –