0

TableFixHeadershttps://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);    
}
4

1 に答える 1

1

のコードはS_ListFragment意味がありません。

フラグメントレイアウトとして使用するように変更したい

のコンテンツビューを、Activity投稿した2番目のレイアウトに設定します。

<?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>

次に、S_ListFragment'onCreateView()メソッドでtable.xmlレイアウトをFragment'ビューとして使用します。

// 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);
    //...

基本的に(これを行うフラグメントを含む)のレイアウトを別のレイアウトに置き換えるため、Activityからのコンテンツビューを再設定することにはあまり意味がありません。また、私は何であるかはわかりませんが、クラスをクラスから切り離すことができないかどうかを確認して、アダプターを作成するときに外部参照を必要としないようにします。FragmentActivityFragmentLayoutTableAdapter

于 2013-03-19T12:51:08.087 に答える