Reputation: 301
I am using TableFixHeaders (https://github.com/InQBarna/TableFixHeaders) - an Android widget library to display tables with headers.
The default table.xml layout for this custom table activity is:
<?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" />
I would like to modify it to be used as a fragment layout, similar to the following example 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>
The code used to populate the custom table list is as follows:
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);
Because I am calling setContentView using the default table.xml layout, I cannot get it to work as a fragment.
The reference to com.inqbarna.tablefixheaders.TableFixHeaders inside the table.xml is important for the table adapter to work.
I would like to ask for help on how to modify the above table.xml layout to change it into the required fragment layout.
Edited code following suggestions from 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);
}
}
Partial code for 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);
}
Upvotes: 0
Views: 2675
Reputation: 87064
Your code in the S_ListFragment
is not making any sense.
I would like to modify it to be used as a fragment layout
Set the content view of the Activity
to the second layout you posted:
<?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>
Then in the S_ListFragment
'onCreateView()
method use the table.xml
layout as the Fragment
's view:
// You can't extend ListFragment unless the view for that fragment contains a
// ListView(and with a special id). If the content of your fragment it's just
// the table.xml layout then you can't extend ListFragment as TableFixHeaders is
// not a ListView(as Selvin pointed out in his comment)
public static class S_ListFragment extends Fragment {
boolean mDualPane;
int mCurCheckPosition = 0;
private TableFixHeaders tableFixHeaders;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundler 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);
FragmentLayout outer = new FragmentLayout();
TableAdapter tableAdapter = outer.new S_Adapter(getActivity());
tableFixHeaders.setAdapter(tableAdapter);
//...
There isn't much sense in re-setting the content view of an Activity
from a Fragment
as you basically replace the layout of the Activity
(containing the fragment from which you do this) with another layout. Also I don't know what FragmentLayout
is but see if you can't detach the TableAdapter
class from it so you don't require the outher reference when creating adapters.
Upvotes: 1