我正在使用TableFixHeaders ( https://github.com/InQBarna/TableFixHeaders ) - 一个 Android 小部件库来显示带有标题的表格。
此自定义表活动的默认table.xml布局是:
<?xml version="1.0" encoding="utf-8"?>
<com.inqbarna.tablefixheaders.TableFixHeaders
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/table"
android:layout_width="match_parent"
android:layout_height="match_parent" />
我想修改它以用作片段布局,类似于以下示例fragment_layout.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">
<fragment class="com.example.fragment.FragmentLayout$S_ListFragment"
android:id="@+id/list"
android:layout_width="match_parent" android:layout_height="match_parent" />
</FrameLayout>
用于填充自定义表格列表的代码如下:
public static class S_ListFragment extends ListFragment {
boolean mDualPane;
int mCurCheckPosition = 0;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Populate list with S_Adpater
getActivity().setContentView(R.layout.table);
FragmentLayout outer = new FragmentLayout();
TableFixHeaders tableFixHeaders = (TableFixHeaders) getActivity().findViewById(R.id.table);
TableAdapter tableAdapter = outer.new S_Adapter(getActivity());
tableFixHeaders.setAdapter(tableAdapter);
因为我使用默认的 table.xml 布局调用 setContentView,所以我无法让它作为片段工作。table.xml中对com.inqbarna.tablefixheaders.TableFixHeaders的引用对于表适配器的工作很重要。
我想请教一下如何修改上面的table.xml布局以将其更改为所需的片段布局。
根据 Luksprog 的建议编辑代码:
public class S_List extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_layout);
}
public static class S_ListFragment extends Fragment {
boolean mDualPane;
int mCurCheckPosition = 0;
private TableFixHeaders tableFixHeaders;
private ETableAdapter tableAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle args) {
// set table.xml as the layout for the fragment's view
View content = inflater.inflate(R.layout.table, container, false);
tableFixHeaders = (TableFixHeaders) content.findViewById(R.id.table);
return content;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
SAdapter outer = new SAdapter();
tableAdapter = outer.new SListAdapter(getActivity());
tableFixHeaders.setAdapter(tableAdapter);
// Check to see if we have a frame in which to embed the details
// fragment directly in the containing UI.
View eventsFrame = getActivity().findViewById(R.id.events);
mDualPane = eventsFrame != null && eventsFrame.getVisibility() == View.VISIBLE;
if (savedInstanceState != null) {
// Restore last state for checked position.
mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
}
if (mDualPane) {
// In dual-pane mode, the list view highlights the selected item.
showDetails(mCurCheckPosition);
}
}
ETableAdapter 的部分代码:
import com.inqbarna.tablefixheaders.adapters.BaseTableAdapter;
/**
* This class implements the main functionalities of the TableAdapter
* @author Brais Gabín
*/
public abstract class ETableAdapter extends BaseTableAdapter {
private final Context context;
private final LayoutInflater inflater;
/**
* Constructor
*
* @param context
* The current context.
*/
public ETableAdapter(Context context) {
this.context = context;
inflater = LayoutInflater.from(context);
}
/**
* Returns the context associated with this array adapter. The context is
* used to create views from the resource passed to the constructor.
*
* @return The Context associated with this adapter.
*/
public Context getContext() {
return context;
}
/**
* Quick access to the LayoutInflater instance that this Adapter retrieved
* from its Context.
*
* @return The shared LayoutInflater.
*/
public LayoutInflater getInflater() {
return inflater;
}
@Override
public View getView(final int row, final int column, View converView, ViewGroup parent) {
if (converView == null) {
converView = inflater.inflate(getLayoutResource(row, column), parent, false);
}
setText(converView, getCellString(row, column));
converView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//....
}
return converView;
}
}
/**
* Sets the text to the view.
*
* @param view
* @param text
*/
private void setText(View view, String text) {
((TextView) view.findViewById(android.R.id.text1)).setText(text);
}
/**
* @param row
* the title of the row of this header. If the column is -1
* returns the title of the row header.
* @param column
* the title of the column of this header. If the column is -1
* returns the title of the column header.
* @return the string for the cell [row, column]
*/
public abstract String getCellString(int row, int column);
public abstract int getLayoutResource(int row, int column);
public abstract int getColOrder(int column);
public abstract void setColOrder(int column, int newOrder);
public abstract void setHeaderId(int column);
public abstract int getHeaderId(int column);
}