我遇到了一些问题,试图弄清楚如何制作我以编程方式添加到LinearLayout可点击的片段。我正在使用片段,因为它们将处于多个活动中,这对我来说是一个很好的方式来创建这样的布局:http://i.stack.imgur.com/P4lOG.png
但是,如果有更好的方法来实现这一目标,那将使其成为可点击的过程,我当然愿意改变现状。
无论如何,我将片段添加到LinearLayout,jobsList,如下所示:
FragmentManager fragmentManager = getFragmentManager();
int count = 2;
for (int i = 0; i < count; i++) {
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
Bundle bundle = new Bundle();
bundle.putInt("jobID", i + 1);
jobFragment job = new jobFragment();
job.setArguments(bundle);
fragmentTransaction.add(R.id.jobsList, job, Integer.toString(i));
fragmentTransaction.commit();
}
2的计数现在只是一个占位符,以后会有任意数量的工作。
这是布局,它有点乱,但我按照我想要的方式得到了所有特定的权重。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rootContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".2" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/basicPort"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background"
android:text="Port" />
<TextView
android:id="@+id/basicLoadOrEmpty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background"
android:text="Load/Empty" />
<TextView
android:id="@+id/basicInOrOut"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background"
android:text="In/Out" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".6"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".333" >
<TextView
android:id="@+id/basicContainerNum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".4"
android:background="@drawable/background"
android:text="Container #" />
<TextView
android:id="@+id/basicChassisNum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".4"
android:background="@drawable/background"
android:text="Chassis #" />
<TextView
android:id="@+id/basicContainerType"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight=".2"
android:background="@drawable/background"
android:text="Cont. Type" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".333" >
<TextView
android:id="@+id/basicDirectionArrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".2"
android:background="@drawable/background"
android:text="Direction" />
<TextView
android:id="@+id/basicCustomer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight=".8"
android:background="@drawable/background"
android:text="Customer Location" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".333" >
<TextView
android:id="@+id/basicSteamshipLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".4"
android:background="@drawable/background"
android:text="Steamship Line" />
<TextView
android:id="@+id/basicBKG_BOL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".6"
android:background="@drawable/background"
android:text="BKG-BOL#" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight=".2" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/basicStatus1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:background="@drawable/background"
android:text="Status 1" />
<TextView
android:id="@+id/basicStatus2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:background="@drawable/background"
android:text="Status 2" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<Space
android:layout_width="match_parent"
android:layout_height="20dp" />
</LinearLayout>
我的问题是,是否有可能使这些片段可以点击并且可以识别,如果是这样,那么最好的方法是什么?
谢谢您的帮助!
你能做的是:
首先 - 片段不可点击,布局和他们的孩子都是。 Fragment
可以处理其布局中发生的事情的单击操作,但是您不“单击”该片段。
第二 - 听起来你对如何使用片段以及Fragment
实际上做了什么感到困惑。一个Fragment
就像一个Activity
,缺少一些部分。 Fragment
有自己的生命周期,可用于执行许多与Activity
相同的任务,不同之处在于:(1)Fragment
由提供缺失功能并且不能自行运行的活动“托管”(2) )Fragment
可以在没有UI的情况下在后台运行(我还不知道背景活动)。
你不需要/不应该使用Fragments
来做你想做的事情。
你的布局真的只是一系列的TextView
s。您应该能够在XML中重复使用该布局文件 - 只需在应用程序中的任何其他位置获取您想要的LayoutInflater
。如果事情变得非常疯狂你可以创建一个自定义的TextView
类来提供详细的功能,但你不应该使用10个片段,每个片段都有一个TextView
来使布局工作。
我只是想跟进一些事情,以防万一有人在将来偶然发现这个问题。事实证明使用片段是一个坏主意。虽然我能够通过一些黑客工作使它们可点击和识别,但是当我试图解决布局问题时,我遇到了问题。所以,多亏了@Rarw,我调查了使用LayoutInflater
。
这更容易!我刚刚制作了布局xml文件,使用LayoutInflater
将其添加到LinearLayout
并将其添加到列表中。让事情变得更加轻松:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout container = new LinearLayout(this);
inflater.inflate(R.layout.basic, container, true);
container.setId(i);
list.addView(container, params);
container.setClickable(true);
container.setOnClickListener(this);
一个hacky解决方案是在整个片段的顶部放置一个透明按钮,让片段扩展onClickListener,并将按钮的onClickListener设置为此。
创建此类按钮的一种方法:使用constraintLayout作为xml的根布局。作为constraintLayout的最后一个子节点,创建一个按钮,通过将layout_constrainXXX设置为角元素来约束它以占据整个片段。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<!-- The remaining properties of the layout -->
<!-- All the children of the layout -->
<Button
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:background="#00000000"
android:id="@+id/fragment_button" />
</android.support.constraint.ConstraintLayout>
请注意,您也可以通过其他方式执行此操作,例如在RelativeLayout
中
然后在java / kotlin文件中,扩展onClickListener。
override fun onClick(p0: View?) {
// Whatever you want to do here
}
在onActivityCreated方法中,将透明按钮的侦听器设置为:
var button : Button = fragment_button
button.setOnClickListener(this)