Messages ListFragment
来源:互联网 发布:python txt转xml 编辑:程序博客网 时间:2024/05/16 17:23
15. Messages ListFragment
For the chat screen create a layout file activity_chat.xml with the following content.<
LinearLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
android:orientation
=
"vertical"
android:background
=
"@android:color/darker_gray"
>
<
fragment
android:name
=
"com.appsrox.instachat.MessagesFragment"
android:id
=
"@+id/msg_list"
android:layout_width
=
"match_parent"
android:layout_height
=
"0dip"
android:layout_weight
=
"1"
/>
<
RelativeLayout
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:background
=
"@android:color/white"
>
<
Button
android:id
=
"@+id/send_btn"
style
=
"?android:attr/buttonStyleSmall"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_alignParentRight
=
"true"
android:text
=
"Send"
/>
<
EditText
android:id
=
"@+id/msg_edit"
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:layout_alignBottom
=
"@+id/send_btn"
android:layout_toLeftOf
=
"@+id/send_btn"
>
</
EditText
>
</
RelativeLayout
>
</
LinearLayout
>
public
class
MessagesFragment
extends
ListFragment
implements
LoaderManager.LoaderCallbacks<Cursor> {
private
OnFragmentInteractionListener mListener;
private
SimpleCursorAdapter adapter;
@Override
public
void
onAttach(Activity activity) {
super
.onAttach(activity);
try
{
mListener = (OnFragmentInteractionListener) activity;
}
catch
(ClassCastException e) {
throw
new
ClassCastException(activity.toString() +
" must implement OnFragmentInteractionListener"
);
}
}
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
adapter =
new
SimpleCursorAdapter(getActivity(),
R.layout.chat_list_item,
null
,
new
String[]{DataProvider.COL_MSG, DataProvider.COL_AT},
new
int
[]{R.id.text1, R.id.text2},
0
);
adapter.setViewBinder(
new
SimpleCursorAdapter.ViewBinder() {
@Override
public
boolean
setViewValue(View view, Cursor cursor,
int
columnIndex) {
switch
(view.getId()) {
case
R.id.text1:
LinearLayout root = (LinearLayout) view.getParent().getParent();
if
(cursor.getString(cursor.getColumnIndex(DataProvider.COL_FROM)) ==
null
) {
root.setGravity(Gravity.RIGHT);
root.setPadding(
50
,
10
,
10
,
10
);
}
else
{
root.setGravity(Gravity.LEFT);
root.setPadding(
10
,
10
,
50
,
10
);
}
break
;
}
return
false
;
}
});
setListAdapter(adapter);
}
@Override
public
void
onActivityCreated(Bundle savedInstanceState) {
super
.onActivityCreated(savedInstanceState);
Bundle args =
new
Bundle();
args.putString(DataProvider.COL_EMAIL, mListener.getProfileEmail());
getLoaderManager().initLoader(
0
, args,
this
);
}
@Override
public
void
onDetach() {
super
.onDetach();
mListener =
null
;
}
public
interface
OnFragmentInteractionListener {
public
String getProfileEmail();
}
}
The layout for the list row is very simple. We already took care of the alignment (differentiating sent and received messages) by setting a ViewBinder to the list adapter.
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
LinearLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
>
<
LinearLayout
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:orientation
=
"vertical"
android:background
=
"@drawable/box"
>
<
TextView
android:id
=
"@+id/text1"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:textAppearance
=
"?android:attr/textAppearanceLarge"
/>
<
TextView
android:id
=
"@+id/text2"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_gravity
=
"right"
android:textAppearance
=
"?android:attr/textAppearanceSmall"
/>
</
LinearLayout
>
</
LinearLayout
>
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
shape
xmlns:android
=
"http://schemas.android.com/apk/res/android"
>
<
solid
android:color
=
"@android:color/white"
/>
<
corners
android:radius
=
"5dp"
/>
<
padding
android:left
=
"10dp"
android:top
=
"5dp"
android:right
=
"10dp"
android:bottom
=
"5dp"
/>
</
shape
>
0 0
- Messages ListFragment
- ListFragment
- ListFragment
- listFragment
- ListFragment
- messages
- Messages
- ListFragment实例
- Android ListFragment
- android Listfragment
- Android ListFragment
- ListFragment+CheckedTextView和ListFragment+CheckBox两种多选效果
- 【Android 开发教程】ListFragment
- ListFragment setDivider的问题
- Android ListFragment实例Demo
- Android ListFragment使用
- ListFragment的使用
- 使用ListFragment显示列表
- 34.Oracle深度学习笔记——12C的AWR初步解读
- 基于python3的杭电HDU ACM AC自动机
- OC的优缺点
- git常用命令
- QuickAdapter重构
- Messages ListFragment
- TP-link 无线路由器WDS设置方法图解_无线桥接设置 wifi无线变有线路由 中转站
- 怎么看java程序的运行时间
- AudioPolicyService 的分析之一
- Android需要精通的技能总结
- Redis持久化
- Android Studio引用mipmap中引用.9图片报错
- 【Data Algorithms_Recipes for Scaling up with Hadoop and Spark】Chapter4 LeftOuterJoin
- 好RESTful API的设计原则