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);
}