使通过编程添加的片段可点击,唯一可识别



我遇到了一些问题,试图找出如何使我以编程方式添加到LinearLayout可点击的片段。我使用的片段,因为他们将在多个活动,这是一个很好的方式,我创建一个布局像这样:https://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>

我的问题是,是否有可能使这些片段可点击和唯一可识别,如果是这样的话,最好的方法是什么?

谢谢你的帮助!

你可以这样做:

  1. 修改你的片段的布局,所以它使用RelativeLayout(现在可能是LinearLayout对吗?)通过修改,我的意思是使用RelativeLayout而不是LinearLayout(更复杂,但我认为更好一点),或者把你的LinearLayout INSIDE RelativeLayout。

  2. 放置一个与片段大小匹配的视图,放在textViews的顶部。给它添加一个onClickListener,在点击时执行代码。

First - fragment是不可点击的,而layout和它们的子元素是可点击的。Fragment可以处理在其布局中发生的事情的点击动作,但你不"点击"片段。

第二-听起来你对如何使用片段和Fragment实际做什么感到困惑。Fragment类似于Activity,只是缺少了一些部分。Fragment有自己的生命周期,可以用来执行许多与Activity相同的任务,不同之处在于(1)Fragment是由一个提供缺失功能的活动"托管"的,而不是自己运行(2)Fragment可以在没有UI的情况下在后台运行(我还不知道后台活动)。

你不需要/不应该使用Fragments来做你想做的事情。

你的布局实际上只是一系列的TextView。你应该能够使布局在XML重用布局文件-通过简单地得到一个LayoutInflater在你的应用程序的任何地方,你想要的。如果事情变得非常疯狂,你可以创建一个自定义的TextView类来提供详细的功能,但你不应该使用10个片段,每个片段都有一个TextView来使布局工作。

我只是想跟进一些事情,以防将来有人偶然发现这个答案。事实证明,使用碎片是个坏主意。虽然我能够通过一些hack工作使它们可点击和可识别,但后来当我试图修复布局问题时,我遇到了问题。所以,感谢@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);

一个简单的解决方案是在整个片段的顶部放置一个透明的按钮,让片段扩展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)

最新更新