【发布时间】:2017-03-30 05:16:44
【问题描述】:
我对 android 开发还很陌生,所以请不要因为愚蠢的问题/错误而抨击我!
我的活动中有一个列表视图,但我想在单击列表视图中的框架时打开一个片段。我有 onclick 工作,但它没有启动新活动。
我将在下面包含我的代码。
这是oncreate
protected void onCreate (Bundle savedInstanceState)
{
//Fragment fragment_blank2=new SomeFragment();
e = myBadData.getData();
super.onCreate(savedInstanceState);
setContentView(R.layout.event_list);
events=(ListView) findViewById(R.id.dayList);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,e);
events.setAdapter(adapter);
events.setOnItemClickListener(this);
}
这是点击
public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l)
{
myBadData.setId(i);
Fragment fr = new event_description();
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.add(R.id.eventdescription, fr);
}
这是片段的 XML
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id = "@+id/eventdescription"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="layout.BlankFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="137dp"
android:text="@string/event_name"
android:textSize="26sp"
android:layout_gravity="top"
android:gravity="top|center"
android:paddingTop="10dp"
android:id="@+id/textView"
android:background="@android:color/holo_orange_light"
android:textColor="@android:color/black"
android:layout_weight="1.08" />
<TextView
android:text="@string/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/date"
android:layout_marginTop="@dimen/activity_top_margin"
android:layout_marginLeft="@dimen/activity_side_margin"
android:textSize="18sp"
android:drawableTint="@android:color/background_dark" />
<TextView
android:text="@string/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/time"
android:layout_marginTop="70dp"
android:layout_marginLeft="@dimen/activity_side_margin"
android:textSize="18sp" />
<TextView
android:text="@string/cost"
android:layout_gravity="right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/cost"
android:layout_marginTop="@dimen/activity_top_margin"
android:layout_marginRight="70dp"
android:textSize="18sp"/>
<TextView
android:text="@string/address"
android:layout_gravity="right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/address"
android:layout_marginTop="70dp"
android:layout_marginRight="60dp"
android:textSize="18sp" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="135dp"
android:layout_marginLeft="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:id="@+id/tv_long"
android:paddingTop="10dp"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="@string/sample_text"
android:textSize="18sp"
android:layout_weight="1.08">
</TextView>
</LinearLayout>
</ScrollView>
<Button
android:text="@string/add_to_calendar"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:id="@+id/button2"
android:layout_gravity="center_horizontal"
android:layout_marginTop="95dp"/>
</FrameLayout>
这里是片段的类
public class event_description extends Fragment
{
@Nullable
//@Override
public View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate (R.layout.event_description, container, false);
Bundle args = getArguments();
String arg = args.getString(eventList.KEY_NAME);
//return super.(R.layout.event_description, container, false);
return view;
}
public View getView() {
return getView();
}
}
【问题讨论】:
-
属于主题,但您可以考虑显示新活动,而不是添加片段,如果这可能会使您的代码更清晰,则在创建时将必要的数据传递给它。..
-
片段的视图组是什么意思?
-
请不要重复提问。只需使用您拥有的任何新信息、您尝试过的任何新代码或任何已发布答案不起作用的解释来编辑您的原始帖子,就会将它推到活动队列的顶部。
标签: android xml listview android-activity fragment