我有这个片段:
ThreatList.java
public class ThreatList extends Fragment {
private TableLayout table;
private TableRow row;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_threat, container, false);
table = view.findViewById(R.id.TableThreat);
if(PcapFile.IsPcapOpen()){
AddHeaders();
AddData(PcapFile.GetIpSource(),
PcapFile.GetIpDestination());
}
return view;
}
public void AddHeaders(){
row = new TableRow(getActivity());
row.setLayoutParams(new TableRow.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
TextView host = new TextView(getActivity());
host.setText("SOURCE");
host.setTextColor(Color.BLACK);
host.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
host.setPadding(5, 5, 5, 0);
row.addView(host);
TextView uri = new TextView(getActivity());
uri.setText("DESTINATION");
uri.setTextColor(Color.BLACK);
uri.setPadding(5, 5, 5, 0);
uri.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
row.addView(uri);
table.addView(row, new TableLayout.LayoutParams(
TableLayout.LayoutParams.FILL_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));
row = new TableRow(getActivity());
row.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
TextView divider = new TextView(getActivity());
divider.setText("__");
divider.setTextColor(Color.BLACK);
divider.setPadding(5, 0, 0, 0);
divider.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
row.addView(divider);
TextView divider2 = new TextView(getActivity());
divider2.setText("__");
divider2.setTextColor(Color.BLACK);
divider2.setPadding(5, 0, 0, 0);
divider2.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
row.addView(divider2);
table.addView(row, new TableLayout.LayoutParams(
TableLayout.LayoutParams.FILL_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));
}
public void AddData(ArrayList<String> source, ArrayList<String> destination){
TextView data;
for (int i = 0; i < source.size(); i++)
{
row = new TableRow(getActivity());
row.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
data = new TextView(getActivity());
data.setText(source.get(i));
data.setTextColor(Color.DKGRAY);
data.setTypeface(Typeface.DEFAULT, Typeface.NORMAL);
data.setPadding(5, 5, 5, 5);
row.addView(data);
data = new TextView(getActivity());
data.setText(destination.get(i));
data.setTextColor(Color.DKGRAY);
data.setPadding(5, 5, 5, 5);
data.setTypeface(Typeface.DEFAULT, Typeface.NORMAL);
row.addView(data);
table.addView(row, new TableLayout.LayoutParams(
TableLayout.LayoutParams.FILL_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));
}
}
}
函数AddData(...)用于在行中添加适当的信息,然后将行添加到表中。但是这些信息是一堆真正的数据(字符串中的IP信息,可能是1000个ip数据)的收集,因此片段采用whyle来完全加载布局的内容:
fragment_threat.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="org.nucleo.ami.appsentinel.TabFragments.ThreatList"
android:background="@color/grey">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
tools:ignore="UselessParent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableLayout
android:id="@+id/TableThreat"
android:layout_width="match_parent"
android:layout_height="match_parent">
</TableLayout>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</ScrollView>
并留下一个空白屏幕,直到片段finsh加载。如何在“加载窗口”中显示片段,同时内容已准备好显示?
我正在使用Tabbed活动的片段,每个标签使用不同的片段来显示自己的数据,但我发现了ThreatList.java片段的问题。
这是我的fragmentpageadapter:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private Fragment http = new HttpList();
private Fragment dns = new DnsList();
private Fragment threat = new ThreatList();
private Fragment resume = new ResumeList();
SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
//return PlaceholderFragment.newInstance(position + 1);
switch (position) {
case 0:
return http;
case 1:
return dns;
case 2:
return threat;
case 3:
return resume;
}
return null;
}
@Override
public int getCount() {
return 4;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getString(R.string.http_tab);
case 1:
return getString(R.string.dns_tab);
case 2:
return getString(R.string.threat_tab);
case 3:
return getString(R.string.resume_tab);
}
return null;
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
答案 0 :(得分:0)
您只需要在 onCreateView()方法中扩充您的布局,并且所有其他组件初始化和操作都将在重写方法 onViewCreated()上执行,以获取片段。
public class ThreatList extends Fragment {
private TableLayout table;
private TableRow row;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_threat, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
table = view.findViewById(R.id.TableThreat);
if(PcapFile.IsPcapOpen()){
AddHeaders();
AddData(PcapFile.GetIpSource(),
PcapFile.GetIpDestination());
}
return view;
}
public void AddHeaders(){
row = new TableRow(getActivity());
row.setLayoutParams(new TableRow.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
TextView host = new TextView(getActivity());
host.setText("SOURCE");
host.setTextColor(Color.BLACK);
host.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
host.setPadding(5, 5, 5, 0);
row.addView(host);
TextView uri = new TextView(getActivity());
uri.setText("DESTINATION");
uri.setTextColor(Color.BLACK);
uri.setPadding(5, 5, 5, 0);
uri.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
row.addView(uri);
table.addView(row, new TableLayout.LayoutParams(
TableLayout.LayoutParams.FILL_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));
row = new TableRow(getActivity());
row.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
TextView divider = new TextView(getActivity());
divider.setText("__");
divider.setTextColor(Color.BLACK);
divider.setPadding(5, 0, 0, 0);
divider.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
row.addView(divider);
TextView divider2 = new TextView(getActivity());
divider2.setText("__");
divider2.setTextColor(Color.BLACK);
divider2.setPadding(5, 0, 0, 0);
divider2.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
row.addView(divider2);
table.addView(row, new TableLayout.LayoutParams(
TableLayout.LayoutParams.FILL_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));
}
public void AddData(ArrayList<String> source, ArrayList<String> destination){
TextView data;
for (int i = 0; i < source.size(); i++)
{
row = new TableRow(getActivity());
row.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
data = new TextView(getActivity());
data.setText(source.get(i));
data.setTextColor(Color.DKGRAY);
data.setTypeface(Typeface.DEFAULT, Typeface.NORMAL);
data.setPadding(5, 5, 5, 5);
row.addView(data);
data = new TextView(getActivity());
data.setText(destination.get(i));
data.setTextColor(Color.DKGRAY);
data.setPadding(5, 5, 5, 5);
data.setTypeface(Typeface.DEFAULT, Typeface.NORMAL);
row.addView(data);
table.addView(row, new TableLayout.LayoutParams(
TableLayout.LayoutParams.FILL_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));
}
}
}
}
希望这有帮助