gpt4 book ai didi

android - 为 EmptyView android 设置一个按钮 onClickListener

转载 作者:行者123 更新时间:2023-11-30 03:34:28 24 4
gpt4 key购买 nike

我正在学习 Android 编程的 Big Nerd Ranch 指南,我正面临第 16 章的挑战。挑战是为 ListView 创建一个 EmptyView,然后在 EmptyView 上创建一个添加内容的按钮。我让 EmptyView 开始工作,但我不知道应该在哪里制作我的按钮。这是我的代码。

public View onCreateView(LayoutInflater inflater, ViewGroup parent, 
Bundle savedInstanceState) {
View v= super.onCreateView(inflater, parent, savedInstanceState);
inflater.inflate(R.layout.list_frame_layout, parent);

return v;
}

这是我的 XML。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

</ListView>

<LinearLayout android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
android:text="@string/empty_no_crime" />

<Button
android:id="@+id/empty_new_crime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/empty_new_crime">
</Button>
</LinearLayout>
</FrameLayout>

这本书告诉我们要使用 fragment ,因此要使用膨胀。我认为代码应该是

mNewCrime=(Button)getView().findViewById(R.id.empty_new_crime)

但这行不通。有什么想法吗?

编辑*:嗯,显然这也不是很好用。当我添加东西时,EmptyView 不会消失,它只是在列出项目时被推下。关于如何在我添加内容后立即让 EmptyView 消失的任何想法?

最佳答案

一开始我也遇到了这个挑战。我想多了!您现在可能已经解决了这个问题,但我认为为其他人发布答案会很有用。以下对我有用:

  1. 创建一个新的 XML 文件,指定“空”和“列表” View ,就像您已经完成的那样。

  2. 修改您现有的 onCreateView 方法以扩充新的修改后的布局,其中包含您在 XML 中定义的“空”和“列表” View 。

  3. 创建一个新按钮并为该按钮设置 onClickListener。

代码如下:

@TargetApi(11)
@Override
// We override the onCreateView to set the subtitle by default if we are rocking >3.0
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)
{
super.onCreateView(inflater, parent, savedInstanceState);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
if(mSubtitleVisible){
getActivity().getActionBar().setSubtitle(R.string.subtitle);
}// End inner if
}// End if

View v = inflater.inflate(R.layout.empty_layout, parent, false);

mNewCrimeButton = (Button)v.findViewById(R.id.add_crime);
//Define an click event listener for the button and launch the new crime fragment when clicked
mNewCrimeButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Crime crime = new Crime();
//Get the crimelab from the activity and add the crime
CrimeLab.get(getActivity()).addCrime(crime); //getActivity returns the activity this fragment is attached to
Intent i = new Intent(getActivity(), CrimePagerActivity.class);
startActivityForResult(i,0);
}//End onClick
});

return v;
}// End onCreateView

这应该适用于您现有的 xml 布局。我希望这有帮助。

关于android - 为 EmptyView android 设置一个按钮 onClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16851754/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com